活动之间不能取布尔值

时间:2019-01-30 21:45:40

标签: android sharedpreferences

我有以下课程来检查我的应用程序的首次启动:

public class PrefManager 
{
    SharedPreferences pref;
    SharedPreferences.Editor editor;
    Context _context;

    // shared pref mode
    int PRIVATE_MODE = 0;

    // Shared preferences file name
    private static final String PREF_NAME = "dnq-welcome";

    private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch";

    public PrefManager(Context context) {
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }

    public void setFirstTimeLaunch(boolean isFirstTime) {
        editor.putBoolean(IS_FIRST_TIME_LAUNCH, isFirstTime);
        editor.commit();
    }

    public boolean isFirstTimeLaunch() {
        return pref.getBoolean(IS_FIRST_TIME_LAUNCH, true);
    }
}

在介绍中,我使用了没有问题的检查:

// Checking for first time launch - before calling setContentView()
prefManager = new PrefManager(this);

if (!prefManager.isFirstTimeLaunch()) {
    launchHomeScreen();
    finish();
}

现在,在第二个活动中,我想继续检查登录过程。所以我几乎有相同的代码,但是没有用。

import eu.healthydev.quizhero.Intro.PrefManager;

public class MainActivity extends AppCompatActivity {

PrefManager prefManager;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

if (mAuth.getCurrentUser() != null) {
            // Checking for first time launch - before calling setContentView()
            if (prefManager.isFirstTimeLaunch()) {
                startActivity(new Intent(MainActivity.this, NewUserActivity.class));
                Toast.makeText(MainActivity.this, "isFirstTimejoin.",
                        Toast.LENGTH_LONG).show();
            }
            else{
                startActivity(new Intent(MainActivity.this, UserProfile.class));
                Toast.makeText(MainActivity.this, "!isFirstTimejoin.",
                        Toast.LENGTH_LONG).show();
            }
        }

我得到空异常。如果我添加

prefManager = new PrefManager(this);

然后第三个活动不会更改prefManager的值

@Override
        public void onClick(View view) {
            if(!TextUtils.isEmpty(editUsername.getText().toString().trim())) {
                uploadUserData();
                Intent intent = new Intent();
                intent.setClass(NewUserActivity.this, GameChooseActivity.class);
                startActivity(intent);
                prefManager.setFirstTimeLaunch(false);
                finishAffinity();
            }
            else{
                FancyToast.makeText(NewUserActivity.this,"Nickname cannot be empty.",FancyToast.LENGTH_SHORT,FancyToast.ERROR,false).show();
            }

否则,第三项活动将正常进行,没有任何问题。所以我的问题是,为什么要参加三个活动中的两个?任何想法? (如果需要更多代码,只需询问)

更新

经过多次尝试,我设法根据需要设置值,现在我遇到了一个极其不可能的事件...:)

//check the current user
        if (mAuth.getCurrentUser() != null) {
            if (!prefManager.isFirstTimeLaunch()) {
                Intent userprofile = new Intent(this, UserProfile.class);
                startActivity(userprofile);
                FancyToast.makeText(MainActivity.this,"!prefManager.isFirstTimeLaunch() User profile should run",FancyToast.LENGTH_SHORT,FancyToast.ERROR,false).show();
            }
            else {
                startActivity(new Intent(MainActivity.this, NewUserActivity.class));
                FancyToast.makeText(MainActivity.this, "New User should run", FancyToast.LENGTH_SHORT, FancyToast.ERROR, false).show();
            }
        }

我看到消息“!prefManager.isFirstTimeLaunch()用户配置文件应运行”,但NewUserActivity重新开始。完全失去了这种情况!

1 个答案:

答案 0 :(得分:0)

在第二个活动中,您需要创建PrefManager的实例。

代替

PrefManager prefManager;//null. 

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            if (mAuth.getCurrentUser() != null) {
            //WARNING. prefManager is null. NullPointer exception is comming.
            if (prefManager.isFirstTimeLaunch()) {
                startActivity(new Intent(MainActivity.this, NewUserActivity.class));
                Toast.makeText(MainActivity.this, "isFirstTimejoin.",
                        Toast.LENGTH_LONG).show();
            }
            else{
                startActivity(new Intent(MainActivity.this, UserProfile.class));
                Toast.makeText(MainActivity.this, "!isFirstTimejoin.",
                        Toast.LENGTH_LONG).show();
            }
        }
}

您可以在onCreate方法中创建PrefManager的实例。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        prefManager = new PrefManager(this);//instance. 
            if (mAuth.getCurrentUser() != null) {
            //ALL OK. prefManager is not null. NullPointerException is avoided.
            if (prefManager.isFirstTimeLaunch()) {
                startActivity(new Intent(MainActivity.this, NewUserActivity.class));
                Toast.makeText(MainActivity.this, "isFirstTimejoin.",
                        Toast.LENGTH_LONG).show();
            }
            else{
                startActivity(new Intent(MainActivity.this, UserProfile.class));
                Toast.makeText(MainActivity.this, "!isFirstTimejoin.",
                        Toast.LENGTH_LONG).show();
            }
        }
}