본문 바로가기

반응형

FRAGMENT

[Android] 안드로이드 Fragment replace할때 onCreate 다시그리기 방지하기 네비게이션 드로어를 사용하다 보면 아래와 같은 방법으로 Fragment replace를 많이 사용하고는 한다. 하지만, 아래와 같은 방법으로 사용했을때 가장 큰 문제점은 Fragment들이 replace될 때마다 onCreate하여 화면을 다시 그린다는 것이다. Fragment 라이프사이클도 찾아보고 방법을 생각해 보았지만 화면을 다시 그리는 상황을 막아야할 때 이 문제를 어떻게 해결해야할지 막막했다. FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.fragment_container, fragment, fragmentTag) .commitAllowingSt.. 더보기
[Android] Fragment 별로 메뉴 구성 다르게 하기 안드로이드 메뉴 구성 할때 Fragment별로 메뉴 구성을 다르게 하기 Activity에서 menu 구성하던 방법과 유사하게 onCreateOptionMenu를 Ovrride해주면 된다. 여기서 주의 할 점은 setHasOptionsMenu(true); 함수를 적어 주어야 한다는 것이다. 메뉴 클릭의 경우에도 onOptionsItemSelected를 Ovrride해주어 아이템 아이디 별로 이벤트를 작성해 주면 된다. 1. FragmentA public class FragmentA extends Fragment { private LinearLayout FragmentA; @Override public View onCreateView(LayoutInflater inflater, ViewGroup contai.. 더보기
[Android] Activity에서 Fragment 함수 호출, Fragment에서 Activity 함수 호출 프라그먼트를 사용하다 보면 Fragment에서 Activity 함수를 호출하는 경우, Activity에서 Fragment 함수를 호출하는 경우가 꽤나 있다. 1. Activity function call from Fragment((MainActivity)getActivity()).testFunction(); 2. Fragment function call from Activity 2.1 findFragmentTag 이용((FragmentB) getSupportFragmentManager().findFragmentByTag("fragmentTag")).testFunction(); 2.2 findFragmentById 이용TestFragment tf = (TestFragment) getSupportFragme.. 더보기

반응형