如何检查是否已跳过firstrun?

时间:2017-05-05 12:45:45

标签: java android

我的第一次活动中有一个按钮,表示“继续”并启动新活动(MainActivity intent),但我发现可以通过重新启动应用程序跳过该按钮和第四次活动​​。所以我想确保不可能跳过这个活动。

这就是我一直在想的:

firstEntry.java - 在这个方法中,我在共享首选项中添加了新值,确认没有跳过firstrun(如果单击该按钮,则只添加值)。

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(skip, false);
editor.commit();
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
finish();
}

MainActivity中的OnCreate()方法

boolean skipper = prefs.getBoolean(skip, false);
        if(skipper == true){
            Intent i = new Intent(getApplicationContext(), firstEntry.class);
            startActivity(i);
        }

OnResume功能:

@Override
    protected void onResume() {
        super.onResume();
        if (prefs.getBoolean("firstrun", true)) {// Checks if application is on its first run
            Intent i = new Intent(getApplicationContext(), firstEntry.class);
            startActivityForResult(i, 1);
            prefs.edit().putBoolean("firstrun", false).commit();
            finish();
        }
    }

如何使用共享首选项值检查共享首选项方法“firstrun”是否跳过了“继续”按钮?

1 个答案:

答案 0 :(得分:1)

更改btn.setOnClickListener中的以下行(new View.OnClickListener()

editor.putBoolean(skip, false);

editor.putBoolean(skip, true); 

然后在firstEntry.java的OnCreate()方法

boolean skipper = prefs.getBoolean(skip, false);
    if(skipper == true){
        Intent i = new Intent(getApplicationContext(), MainActivity.class);

        Log.i("firstEntry.java","skipped the activity");
        startActivity(i);
}