Reputation: 1531
I want to recreate activity after navigation drawer menu is clicked and close this drawer.
I've tried this:
private void drawerItemClicked(int position) {
drawerLayout.closeDrawer(drawer);
//...
recreate();
}
but doesn't work well. Drawer is starting to close, but screen blacks out for sec and activity is recreated with opened drawer.
Do I have to handle drawer state by myself with onSaveInstanceState()
or there is other way?
Upvotes: 2
Views: 1092
Reputation: 11
I know it's an old question, but maybe my solution can be of use to someone else. I ran into the same issue today.
Apparently the problem is that the framework has not completely closed the navigation drawer when the activity is recreated. Therefore I moved the recreate() call to the onDrawerClosed listener, like this:
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
//...
if(...)
recreate();
}
It works perfectly and the drawer stays put when the activity restarts.
Upvotes: 1
Reputation: 3389
To make sure that you cleanly recreate your Activity without worrying about the framework handling something in an unexpected way with recreate()
you can explicitly declare a fresh new instance of the Activity by creating a new Intent. Something like
startActivity(new Intent(this, CurrentActivity.class));
finish();
Calling finish()
after you start the new Activity via the Intent, will finish the underlying (paused state) Activity. This will essentially "recreate" your Activity.
Upvotes: 1