I made a working navigation drawer like it's shown in the tutorial on the developer.android.com website. But now, I want to use one Navigation Drawer, i created in the NavigationDrawer.class for multiple Activities in my Application.
My question is, if anyone here can make a little Tutorial, which explains, how to use one Navigation drawer for multiple Activities.
I read it first at this Answer Android Navigation Drawer on multiple Activities
but it didn't work on my Project
public class NavigationDrawer extends Activity {
public DrawerLayout drawerLayout;
public ListView drawerList;
private ActionBarDrawerToggle drawerToggle;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_drawer, 0, 0) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(R.string.app_name);
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(R.string.menu);
}
};
drawerLayout.setDrawerListener(drawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
layers = getResources().getStringArray(R.array.layers_array);
drawerList = (ListView) findViewById(R.id.left_drawer);
View header = getLayoutInflater().inflate(R.layout.drawer_list_header, null);
drawerList.addHeaderView(header, null, false);
drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, android.R.id.text1,
layers));
View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.drawer_list_footer, null, false);
drawerList.addFooterView(footerView);
drawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
map.drawerClickEvent(pos);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
}
In this Activity i want to have the Navigation Drawer so I extends 'NavigationDrawer' and in some other Activities i want to User the Same Navigation drawer
public class SampleActivity extends NavigationDrawer {...}
I don't know what to change...
Create Navigation drawer in your MainActivity using fragment.
Initialize the Navigation Drawer in MainActivity
now in all other activities you want to use same Navigation Drawer put DrawerLayout as base and fragment as navigation drawer. Just set android:name in your fragment pointing to your fragment Java file. You won't need to initialize the fragment in other Activities.
You can access Nav Drawer by swipe in other activities like in Google Play Store app
I've found the best implementation. It's in the Google I/O 2014 app.
They use the same approach as Kevin's. If you can abstract yourself from all unneeded stuff in I/O app, you could extract everything you need and it is assured by Google that it's a correct usage of navigation drawer pattern. Each activity optionally has a
DrawerLayout
as its main layout. The interesting part is how the navigation to other screens is done. It is implemented inBaseActivity
like this:This differs from the common way of replacing current fragment by a fragment transaction. But the user doesn't spot a visual difference.
For anyone else looking to do what the original poster is asking, please consider to use fragments instead the way Kevin said. Here is an excellent tutorial on how to do that:
https://github.com/codepath/android_guides/wiki/Fragment-Navigation-Drawer
If you choose to instead use activities instead of fragments you are going to run into the problem of the nav drawer being re-created every time you navigate to a new activity. This results in an ugly/slow rendering of the nav drawer each time.
use this for your toolbar.xml
use this for navigation header if want to use
Easiest way to reuse a common Navigation drawer among a group of activities
app_base_layout.xml
AppBaseActivity.java
There is a work around: In my implementation: I have fragments in first activity that has Navigation Drawer. The other activities create the ActionBar and Navigation Drawer from the same code as first activity, and they do not have fragments, the laying out is described later here. In the first activity, the action click of the item is handled in the same activity, and in the other activities, the action is completed later, first I switch to the first activity, by closing the other activities. The conditions are as follows:
I have put the main layout and inside first framelayout, and second framelayout is dedicated to the Drawer in DrawerLayout.
I have done the drawer initialization in onResume of the activities other than the first one. This is because it needs to re-initialize after on new intent updates, as all the activities are single instance.
This way I have been able to have NavigationDrawer and ActionBarCompat in all the activities.
The other way around could be: do not use activities at all, instead use fragments, and replace them in the container (Linear Layout for example) where you show your first fragment.
Code is available in Android Developer Tutorials, you just have to customize.
http://developer.android.com/training/implementing-navigation/nav-drawer.html
It is advisable that you should use more and more fragments in your application, and there should be only three activities local to your application, that you mention in your AndroidManifest.xml apart from the external ones (FacebookActivity for example):
SplashActivity: uses no fragment, and uses FullScreen theme.
LoginSignUpActivity: Do not require NavigationDrawer at all, so simply use normal tool bar, but at the least, 3 or 4 fragments will be required. Uses noactionbar theme
HomeActivity or DashBoardActivity: Uses noactionbar theme. Here you require Navigation drawer, also all the screens that follow will be fragments or nested fragments, till the leaf view. All the settings, user profile and etc. will be here as fragments, in this activity.
[ For further guidance see: https://stackoverflow.com/a/51100507/787399 ]
Happy Coding !!