public class MainActivity extends AppCompatActivity {
MainFragment mainFragment;
MenuFragment menuFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(R.id.mainFragment);
menuFragment = (MenuFragment) getSupportFragmentManager().findFragmentById(R.id.menuFragment);
// menuFragment = new MenuFragment();
}
public void onFragMentChanged(int index) {
if( index == 0 ) {
getSupportFragmentManager().beginTransaction().replace(R.id.container, mainFragment).commit();
}
else if ( index == 1) {
getSupportFragmentManager().beginTransaction().replace(R.id.container, menuFragment).commit();
}
}
}
MainActivity.java
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/mainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.test.MainFragment"/>
<fragment
android:id="@+id/menuFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.test.MenuFragment"/>
</FrameLayout>
activity_main.xml
public class MainFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_main, container, false);
Button button = rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity activity = (MainActivity) getActivity();
activity.onFragMentChanged(1);
}
});
return rootView;
}
}
MainFragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A8DEF6"
android:orientation="vertical"
tools:context=".MainFragment">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="메인 프래그먼트"
android:textSize="50sp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="화면 전환" />
</LinearLayout>
fragment_main.xml
public class MenuFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_menu, container, false);
Button button = rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity activity = (MainActivity) getActivity();
activity.onFragMentChanged(0);
}
});
return rootView;
}
}
MenuFragment.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#99F19D"
android:orientation="vertical"
tools:context=".MainFragment">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="메뉴 프래그먼트"
android:textSize="50sp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="돌아가기" />
</LinearLayout>
fragment_menu.xml
안드로이드 초보,,프래그먼트 공부중입니다..영상보면서 공부중인데 이해가 안되는 부분이 있어서
질문드립니다. 원래 코드는 화면전환 버튼을 누르면 다른 프래그먼트를 보여주고 그 프래그먼트 화면에서 돌아가기 버튼을 누르면 다시 이전 프래그먼트를 보여주는 코드입니다.
즉 해당 프래그먼트에서 버튼을 누르면 프래그먼트 두개를 서로 계속해서 전환해서 보여주는 것입니다.
공부중에 MainActivity.java 파일의 주석 처리한
menuFragment = new MenuFragment(); 부분이 원본 코드이고
getSupportFragmentManager().findFragmentById(R.id.menuFragment);
이 부분이 제가 넣은부분 입니다, 또한
activity_main.xml의 아이디가 menu_fragment 인 프래그먼트 태그도 마찬가지로 제가 넣은것입니다.
이걸 왜 넣었냐면 주석처리한 부분이 왜 있는지 이해가 가질 않기때문입니다..
new를 해준것보니 객체를 생성했다는 말인데..제가 공부하기로는 인플레이션하는 과정이 메모리에
올라가 객체가 만들어지는 과정이라고 배웠거든요..
근데 MenuFragment.java파일이나 MainFragment.java 파일이나 보시다시피 둘다 인플레이션을 해주는데
Menu프래그먼트만 new를 해주는 이유가 무엇인가요?
제가 추가한 코드를 넣고하니 MainFragment가 먼저 뜨지않고 MenuFragment 화면이 먼저뜨고 버튼을 누르니 앱이 죽어버리더군요..
main 프래그먼트든 menu프래그먼트든 소슨느 똑같이 짰고 xml도 똑같이 짰고 뭐..아이디 이런것만 다르지
두개다 똑같이 만들었으니 버튼 클릭할때 인덱스만 다르게줘서 해줘도 충분히 실행가능할거라 생각했는데
바로 죽어버리니 이해하기가 어렵네요..
프래그먼트 너무 어렵네요 ㅠ
--------------------------------------------------------------------
원본 MenuFragment.java 입니다
public class MenuFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_menu, container, false);
}
}
그리고 본문에서도 언급했듯이 주석이나 activity_main.xml 부분에서 아이디가 menu_fragment 인 프래그먼트 태그가 없는것이 원래 영상에서 진행했던 코드입니다.
참고바랍니다 감사합니다