2016-04-15 55 views
-4

我试图添加一些介绍屏幕,只会运行应用程序第一次启动后,它将直接加载登录页面 我使用以下指南来实现这第一次应用程序启动介绍错误(viewpager)

SharedPreferences sp = getSharedPreferences(MyPrefs, 0); 
    if (sp.getBoolean("first", true)) { 
     SharedPreferences.Editor editor = sp.edit(); 
     /*editor.putBoolean("first", false);*/ 
     sp.edit().putBoolean("first", false).commit(); 
     editor.commit(); 
     Intent intent = new Intent(this, login.class); //call your ViewPager class 
     startActivity(intent); 
    } 

但应用程序将跳过开头部分,并加载使用首次应用程序时的登录页面,并再次 启动时加载的介绍页我怎么能扭转这种 感谢

+0

你可以改变if(sp.getBoolean(“first”,true))if(sp.getBoolean(“first”,false))并且让我知道发生了什么:)我会解释你为什么后来:)虽然我可以建议一个更好的方式来实现它:)它只是一个补丁到您的代码:) –

+0

更改条件 –

+0

@SandeepBhandari感谢您的回复,将其更改为false确实有助于引入介绍屏幕,但在下次启动应用程序时它们仍显示出来。 – 7rocker

回答

0
SharedPreferences sp = getSharedPreferences(MyPrefs, 0); 
    if(sp.contains("first"){ 
     Intent intent = new Intent(this, login.class); //call your ViewPager class 
     startActivity(intent); 
    } 
    else{ 
     SharedPreferences.Editor editor = sp.edit(); 
     sp.edit().putBoolean("first", true).commit(); 
     editor.commit(); 
    } 

说明: 当您启动您的应用程序首次变量“第一”将不会出现在共享偏好:)因为你getBoolean指定的默认值的情况下,回到“第一”不发现是真的:)所有你的代码是折腾:)

正确的方式来做到这一点:) 检查首先,如果“第一”关键词是否存在?如果不是这意味着你第一次启动它显示介绍屏幕:)但不要忘记首先输入值真正的:)

因此,下一次启动第一个关键将出现在共享首选项所以现在你知道你是第二次启动:)你可以跳过介绍并启动登录活动:)

不要打扰你的自我与“第一”和所有:)的价值所有你需要知道的是如果这个键存在或不是所有:)

+0

非常感谢@Sandeep Bhandari现在的工作! – 7rocker

1

你在你的情况下有问题。试试这个代码:

SharedPreferences sp = getSharedPreferences(MyPrefs, 0); 
if (sp.getBoolean("first", true)) { 
    sp.edit().putBoolean("first", false).apply(); 

    // Show Intro Activity 
} else { 
    Intent intent = new Intent(this, login.class); 
    startActivity(intent); 
} 

首先你检查一下是否是第一次。如果是,那么您将显示介绍Activity。如果这不是第一次,那么你显示登录Activity

注意我删除了一些SharedPreferences代码,其中一部分是多余的。我也将commit()更改为apply()

相关问题