Android
[Android][Java] Activity 코드를 Fragment로 변경하기
연모링
2022. 5. 24. 14:25
728x90
구글링해서 나오는 예제는 Activity가 많은데 졸업 프로젝트를 Fragment로 하게 됐다.
예제의 Activity코드를 내 프로젝트에 Fragment로 옮겨오는 법...
1. onCreate의 내용은 onCreateView에 넣는다.
2. findViewById 앞에 뷰 적기
Activity일 때
button = (Button)findViewById(R.id.button);
Fragment일 때
View myView = inflater.inflate(R.layout.fragment_ocr, container, false);
galleryBtn = (Button)myView.findViewById(R.id.galleryBtn);
3.resultCode를 쓴다면 이것도 앞에 액티비티 적어주기
Activity
resultCode == RESULT_OK
Fragment
resultCode == Activity.RESULT_OK
4. getContentResolver() 앞에도 getActivity()를 적어준다.
getActivity().getContentResolver()
728x90