Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open NavigationDrawer only on first app start?

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.

like image 572
TheLettuceMaster Avatar asked Aug 27 '13 21:08

TheLettuceMaster


1 Answers

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();
}
like image 150
Philipp Jahoda Avatar answered Nov 14 '22 22:11

Philipp Jahoda