Issue
I'm reading code from the official (and quite outdated) sample about the Nav Drawer, that you can easily download from your IDE [in Android Studio, it's automatically imported if you choose to create a new Navigation Drawer Activity].
I'm really curious about why this actually works. One of the goals here is to have the Drawer open on start-up, but only if the user has not learned how to slide it in yet.
So - on create the drawer shows up, and you can manually close it. Anyway you haven't learned about it yet; the code requires you to manually open the drawer, at least one time, then it will never show up again. This is done using SharedPreferences
, which I know about, and a local boolean, mUserLearnedDrawer
.
I'll post some code along with my question. This is hosted in the fragment class that will be loaded into the drawer. Here we say that mUserLearnedDrawer should be either what is to be found in the preferences, or false:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);
if (savedInstanceState != null) { mFromSavedInstanceState = true; }
In the activity onCreate, we set up the ActionBarDrawerToggle
and call:
//..other stuff
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) { return; }
if (!mUserLearnedDrawer) {
// The user manually opened the drawer; store this flag to prevent auto-showing
// the navigation drawer automatically in the future.
mUserLearnedDrawer = true;
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getActivity());
sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).commit();
}
getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
};
// If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
// per the navigation drawer design guidelines.
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
Question1: what's the point of requiring
mFromSavedInstanceState
to be false?Question2: shouldn't
openDrawer()
at the end callonDrawerOpened
and thus setmUserLearnedDrawer = true
? This would make the first, automatic drawer opening be interpreted as a user swipe into the screen, which is not. In fact, this does not happen - after the drawer shows up on start-upmUserLearnedDrawer
is still false. Why?
I know this is quite unusual question, I'm asking out of mere curiosity.
Solution
Question1: what's the point of requiring mFromSavedInstanceState to be false?
you activity might get recreated on config change (like screen rotation), this variable mFromSavedInstanceState
will prevent opening drawer in such case if user has not learnt on its use yet.
When mFromSavedInstanceState is false, then this is the first time you activity onCreate method is being called, right after user has started it. This is when drawer should be opened automatically, but only if user has not opened it manually (means learned how to do this), which is remembered in mUserLearnedDrawer .
Question2:
I dont remember this code entirely, but mUserLearnedDrawer should be set to true only after user have opened drawer explicitly manually. It is set actually in onDrawerOpened. As you can see your initial mDrawerLayout.openDrawer(mFragmentContainerView);
is called before the setDrawerListener
call, so initially onDrawerOpened will not get called, only later on once user manually opens it.
Answered By - marcinj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.