问题
Per Google guidelines, it is recommended you open the DrawerLayout
the first time only after app is installed and opened (to show user the functionality).
How would you go about doing this?
It seems it would be a combination of openDrawer()
method with some type of preference.
回答1:
I would recommend that you use the SharedPreferences for that:
The basic idea is that you read the SharedPreferences and look for a boolean value that doesn't exist there at first app start. By default, you will return "true" if the value you were looking for could not be found, indicating that it is in fact the first app start. Then, after your first app start you will store the value "false" in your SharedPreferences, and upon next start, the value "false" will be read from the SharedPreferences, indicating that it is no longer the first app start.
Here is an example of how it could look like:
@Override
protected void onCreate(Bundle savedInstanceState) {
// your other code...
// setContentView(...) initialize drawer and stuff like that...
// use thread for performance
Thread t = new Thread(new Runnable() {
@Override
public void run() {
SharedPreferences sp = Context.getSharedPreferences("yoursharedprefs", 0);
boolean isFirstStart = sp.getBoolean("key", true);
// we will not get a value at first start, so true will be returned
// if it was the first app start
if(isFirstStart) {
mDrawerLayout.openDrawer(mDrawerList);
Editor e = sp.edit();
// we save the value "false", indicating that it is no longer the first appstart
e.putBoolean("key", false);
e.commit();
}
}
});
t.start();
}
来源:https://stackoverflow.com/questions/18475419/how-to-open-navigationdrawer-only-on-first-app-start