As far as I understand when clicking on the item , which are on the side, we open the fragment.
I am interested in the following question: how to add a fragment to the main activity and replace it when you click on one of the items?
When working with the Navigation Drawer + Fragments bundle, you can select two mechanisms for the application to work:
When you select a specific fragment in the Navigation Drawer , the current visible fragment is hidden ( hide ) and the selected fragment is displayed ( show ).
When a specific fragment is selected in the Navigation Drawer , the current visible fragment is replaced ( replace ) with the selected fragment ( see explanation below ).
The principal difference between these two approaches is that in the first case, the hidden fragment does not change its state and the methods of its life cycle (when it is hidden and displayed ) are not invoked. In the second case, for a fragment that is replaced, the methods of its life cycle will be sequentially called:
onPause() ;onStop() ;onDestroyView() ;onDestroy() ;onDetach() .Consequently, in the first case, when returning to the hidden fragment, we will see its state at the moment of its concealment , and in the second case, the fragment will be created from scratch, that is, the data that were on it will not be seen.
( explanation ): Frankly speaking, I can’t say in a nutshell what situation you can, or rather, you need to apply the second approach (exactly when changing fragments from Navigation Drawer ) ... but technically it is possible (and in some cases it can even need just such a logic of work).
In the overwhelming majority of cases, the first approach is used, because it is the most logical when using Navigation Drawer + Fragments : the user can switch between screens without losing data on them.
Suppose there are two fragments: FragmentA and FragmentB , below I will give two methods: the first ( changeFragment(...) ) is used to switch ( hide / show ) fragments without losing data, and the second ( replaceFragment(...) ) - to replace one fragment to another.
private void changeFragment(String neededToShowFragmentTag) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); List<Fragment> existingFragments = fragmentManager.getFragments(); Fragment currentShownFragment = null; if (existingFragments != null) { for (Fragment fragment : existingFragments) { if (fragment.isVisible()) { currentShownFragment = fragment; break; } } } if (currentShownFragment == null || !currentShownFragment.getTag().equals(neededToShowFragmentTag)) { Fragment neededToShowFragment = fragmentManager.findFragmentByTag(neededToShowFragmentTag); if (neededToShowFragment == null) { switch (neededToShowFragmentTag) { case "fragment_a": neededToShowFragment = new FragmentA(); break; case "fragment_b": neededToShowFragment = new FragmentB(); break; } fragmentTransaction.add(R.id.fragments_container, neededToShowFragment, neededToShowFragmentTag); } if (currentShownFragment != null) { fragmentTransaction.hide(currentShownFragment); } fragmentTransaction.show(neededToShowFragment).commit(); } } private void replaceFragment(String neededToShowFragmentTag) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); Fragment neededToShowFragment = fragmentManager.findFragmentByTag(neededToShowFragmentTag); if (neededToShowFragment == null) { switch (neededToShowFragmentTag) { case "fragment_a": neededToShowFragment = new FragmentA(); break; case "fragment_b": neededToShowFragment = new FragmentB(); break; } if (fragmentManager.getFragments() == null) { fragmentTransaction.add(R.id.fragments_container, neededToShowFragment, neededToShowFragmentTag); } else { fragmentTransaction.replace(R.id.fragments_container, neededToShowFragment, neededToShowFragmentTag); } fragmentTransaction.commit(); } } Next, I place the layout files and the code of all the classes so that you can easily assemble all this and see the difference.
menu_main.xml :
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_main_fragment_a" android:title="Фрагмент А"/> <item android:id="@+id/menu_main_fragment_b" android:title="Фрагмент В"/> </menu> activity_main.xml :
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <FrameLayout android:id="@+id/fragments_container" android:layout_width="match_parent" android:layout_height="match_parent"/> <android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:menu="@menu/menu_main"/> </android.support.v4.widget.DrawerLayout> fragment_a.xml :
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical"> <TextView android:id="@+id/fragment_a_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Фрагмент A"/> <Button android:id="@+id/fragment_a_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Do it!"/> </LinearLayout> </FrameLayout> fragment_b.xml :
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical"> <TextView android:id="@+id/fragment_b_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Фрагмент B"/> <Button android:id="@+id/fragment_b_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Do it!"/> </LinearLayout> </FrameLayout> FragmentA :
public class FragmentA extends Fragment { private TextView mTextView; private Button mButton; private int i = 0; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_a, container, false); mTextView = (TextView) view.findViewById(R.id.fragment_a_text_view); mButton = (Button) view.findViewById(R.id.fragment_a_button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { i++; mTextView.setText("Фрагмент A : " + i); } }); return view; } } FragmentB :
public class FragmentB extends Fragment { private TextView mTextView; private Button mButton; private int i = 0; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_b, container, false); mTextView = (TextView) view.findViewById(R.id.fragment_b_text_view); mButton = (Button) view.findViewById(R.id.fragment_b_button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { i++; mTextView.setText("Фрагмент B : " + i); } }); return view; } } MainActivity :
public class MainActivity extends AppCompatActivity { private final String FRAGMENT_A_TAG = "fragment_a"; private final String FRAGMENT_B_TAG = "fragment_b"; private DrawerLayout mDrawerLayout; private NavigationView mNavigationView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mNavigationView = (NavigationView) findViewById(R.id.navigation_view); mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem item) { int itemId = item.getItemId(); switch (itemId) { case R.id.menu_main_fragment_a: replaceFragment(FRAGMENT_A_TAG); mDrawerLayout.closeDrawers(); return true; case R.id.menu_main_fragment_b: replaceFragment(FRAGMENT_B_TAG); mDrawerLayout.closeDrawers(); return true; } return false; } }); } private void changeFragment(String neededToShowFragmentTag) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); List<Fragment> existingFragments = fragmentManager.getFragments(); Fragment currentShownFragment = null; if (existingFragments != null) { for (Fragment fragment : existingFragments) { if (fragment.isVisible()) { currentShownFragment = fragment; break; } } } if (currentShownFragment == null || !currentShownFragment.getTag().equals(neededToShowFragmentTag)) { Fragment neededToShowFragment = fragmentManager.findFragmentByTag(neededToShowFragmentTag); if (neededToShowFragment == null) { switch (neededToShowFragmentTag) { case "fragment_a": neededToShowFragment = new FragmentA(); break; case "fragment_b": neededToShowFragment = new FragmentB(); break; } fragmentTransaction.add(R.id.fragments_container, neededToShowFragment, neededToShowFragmentTag); } if (currentShownFragment != null) { fragmentTransaction.hide(currentShownFragment); } fragmentTransaction.show(neededToShowFragment).commit(); } } private void replaceFragment(String neededToShowFragmentTag) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); Fragment neededToShowFragment = fragmentManager.findFragmentByTag(neededToShowFragmentTag); if (neededToShowFragment == null) { switch (neededToShowFragmentTag) { case "fragment_a": neededToShowFragment = new FragmentA(); break; case "fragment_b": neededToShowFragment = new FragmentB(); break; } if (fragmentManager.getFragments() == null) { fragmentTransaction.add(R.id.fragments_container, neededToShowFragment, neededToShowFragmentTag); } else { fragmentTransaction.replace(R.id.fragments_container, neededToShowFragment, neededToShowFragmentTag); } fragmentTransaction.commit(); } } } Source: https://ru.stackoverflow.com/questions/593285/
All Articles