第一次应用启动介绍错误(viewpager)

时间:2016-04-15 09:05:41

标签: android android-viewpager boolean splash-screen

我试图添加一些仅在第一次启动应用程序时才会运行的介绍屏幕,之后它将直接加载登录页面 我使用以下指南来实现这个目标

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

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

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

+(DownloadThumbnail *)sharedDownloadThumbnailInstance{
    if(downloadThumbnail==nil){
        downloadThumbnail = [[DownloadThumbnail alloc]init];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectedToInternet) name:NotifyInternetConnection object:nil];

    }
    return downloadThumbnail;
}   

<强>说明: 第一次启动应用程序时,变量“first”将不会出现在共享首选项中:)因为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(); } 指定在“first”未找到为真的情况下返回的默认值:)所有你的代码是要折腾的:)

正确的方法:) 首先检查“第一个”关键词是否存在?如果不是这意味着你第一次启动它就会显示介绍屏幕:)但是不要忘记先输入密钥,值为true :)

因此,下次启动第一个密钥将出现在共享首选项中所以现在您知道您正在第二次启动:)您可以跳过介绍并启动登录活动:)

不要用“first”和所有:)的值来打扰你的自己所有你需要知道的是,如果这个键存在或者不是全部:)

相关问题