How to open NavigationDrawer only on first app start?

帅比萌擦擦* 提交于 2019-12-21 02:07:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!