如何仅在首次启动应用程序时打开NavigationDrawer? [英] How to open NavigationDrawer only on first app start?

查看:90
本文介绍了如何仅在首次启动应用程序时打开NavigationDrawer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Google指南,建议您仅在安装并打开应用后才第一次打开DrawerLayout(向用户显示功能).

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).

您将如何去做?

这似乎是openDrawer()方法与某种类型的首选项的组合.

It seems it would be a combination of openDrawer() method with some type of preference.

推荐答案

我建议您为此使用 SharedPreferences :

基本想法是,您阅读SharedPreferences并查找在应用程序首次启动时不存在的布尔值. 默认情况下,如果您要查找的值将返回"true" 找不到,表明它实际上是第一个应用程序启动.然后,在您的第一个应用程序启动后,您将存储值 您的SharedPreferences中的值为"false",并且在下次启动时,该值为 从SharedPreferences中读取"false",表明它是 不再是第一个应用启动.

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.

以下是它的外观示例:

@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();
}

这篇关于如何仅在首次启动应用程序时打开NavigationDrawer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆