Run activity only once, then always run the main one

后端 未结 4 1733
北海茫月
北海茫月 2021-01-16 20:11

As the title says, Scenario is: On first time using the app, Show Screen A. Once you are done with screen A, the button will lead to you Screen B. From now on and forever, t

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-16 21:01

    Just declare your Activity A as a launcher Activity in your AndroidManifest.xml and inside your Activity A onCreate() you can simply do this

    private SharedPreferences mSharedPreferences;
    private Editor mEditor;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
    
        mSharedPreferences = getSharedPreferences("yourPrefsFileName", Context.MODE_PRIVATE));
        mEditor = mSharedPreferences.edit();
    
        if (mSharedPreferences.getBoolean("isfirstTime", true)) {
            mEditor.putBoolean("isFirstTime",false);
            mEditor.apply();
        }else{
             startActivity(new Intent(this, ActivityB.class));
             overridePendingTransition(0, 0);
             finish();
          }
    }
    

提交回复
热议问题