To do this, you should not start a new Activity when you click on an item in the Drawer navigation. What you should do is work with fragments in Mainactivity and replace this fragment when an item is selected instead of starting a new Activity.
In your activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar" />
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:theme="@style/NavigationDrawerStyle"
app:menu="@menu/drawer">
</android.support.design.widget.NavigationView>
In your Mainactivity
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()) {
//Replacing the main content with right fragment;
case R.id.start:
if (startfragment == null) {
startfragment = new StartFragment();
}
fragmentTransaction.replace(R.id.frame, startfragment);
fragmentTransaction.commit();
return true;
case R.id.second:
if (secondfragment == null) {
secondfragment = new Secondfragment();
}
fragmentTransaction.replace(R.id.frame, secondfragment);
fragmentTransaction.commit();
return true;
default:
if (startfragment == null) {
startfragment = new StartFragment();
}
fragmentTransaction.replace(R.id.frame, startfragment);
fragmentTransaction.commit();
return true;
}
}
});
And the model of a fragment
public class Startfragment extends Fragment {
//Ui Elements
//Atributtes
private View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_start, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}}
Search on Fragments, Voce will have only one activity and several Fragments that exchange the internal content of the activity. Your question is too wide to offer an answer code..
– leofontes
I’m gonna go check it out, thanks
– Igor Oliveira
I wouldn’t use a fragment because it limits the application too much. You can do a Basectivity, the other activities that need the navigation Drawer should extend it. At the beginning of a little work, but then everything becomes easier. Take a look at this example: http://mateoj.com/2015/06/21/adding-toolbar-and-navigation-drawer-all-activities-android/ I was having the same difficulty as you and that’s where I found the path.
– Cristiano Silva