I had an application with quite a few activities before it was decided that we will be using a Navigation Drawer or a hamburger menu. I did not want to redo the whole app using fragments so I decided to go with the approach used in this answer: Same Navigation Drawer in different Activities
EDIT: And now, this one https://stackoverflow.com/a/23477100/1371585
And I created a base activity called NavDrawerBaseActivity
. Here is the code:
public class NavDrawerBaseActivity extends MyBaseActivity {
public DrawerLayout mNavDrawerLayout;
public ListView mDrawerList;
public String[] mMenuItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.navdrawer_activity);
mNavDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mMenuItems = getResources().getStringArray(R.array.optionsmenu_array);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mMenuItems);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener(this));
super.onCreate(savedInstanceState);
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
private DrawerItemClickListener(Context context) {
mContext = context;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String textClicked = ((TextView) view).getText().toString();
view.setSelected(true);
if (textClicked.equalsIgnoreCase(mContext
.getString(R.string.optionsmenu_library))) {
Intent libraryIntent = new Intent(mContext,
LibraryActivity.class);
libraryIntent.putExtra("navdrawerposition", position);
startActivity(libraryIntent);
mNavDrawerLayout.closeDrawers();
mDrawerList.setItemChecked(position, true); //Not working
} else if (textClicked.equalsIgnoreCase(mContext
.getString(R.string.optionsmenu_settings))) {
// TODO: open activity and close the drawer
} else if (textClicked.equalsIgnoreCase(mContext
.getString(R.string.optionsmenu_logout))) {
// TODO: open activity and close the drawer
}
}
private Context mContext;
}
}
Here is the layout file navdrawer_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="@+id/activity_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
/>
</android.support.v4.widget.DrawerLayout>
Every activity extends the NavDrawerBaseActivity
and does not use setContentView
, like so:
public class LibraryActivity extends NavDrawerBaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Not setting content view here, since its already set in
// NavDrawerBaseActivity
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.activity_frame);
// Inflating the Camera activity layout
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View activityView = layoutInflater
.inflate(R.layout.library_page, null, false);
// Adding the custom layout of this activity to frame layout set in
// NavDrawerBaseActivity.
frameLayout.addView(activityView);
// Only this part of the code is doing what I want.
// int drawerSelectedPosition = getIntent().getIntExtra(mNavDrawerPosExtraName, -1);
// if(drawerSelectedPosition > -1){
// mDrawerList.setItemChecked(drawerSelectedPosition, true);
// }
}
}
My problem: How do I correctly highlight the current activity in the NavDrawer View?
The mDrawerList.setItemChecked(position, true);
before launching Intent or after launching it is not working.
The weird part is: If I am currently in Activity1, Open the NavDrawer and select Activity2. I land in Activity2, open the NavDrawer and see that "Activity2" is not selected. I click the Back button, land in Activity1, open the NavDrawer and see that "Activity2" is selected.
Which means setItemChecked
works, but not in the new activity that gets launched.
Currently I am passing the position as an Intent extra and specifically setting the checked position, like the commented section in LibraryActivity
.
This works but seems like a work around. Please tell me if there is a correct/better way of doing that in NavDrawerBaseActivity
class instead of in each Activity that extends it.