Run activity only once, then always run the main one

后端 未结 4 1745
北海茫月
北海茫月 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 20:55

    public class FirstActivity extends Activity {
    
       public void onCreate(Bundle saved){
          super.onCreate();
    
          SharedPreferences prefs = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
    
         if (!prefs.getBoolean("firstStart", true)){
              startActivity(this, SecondActivity.class);
              finish(); // Finish the current one, is like forwarding directly to the second one
          }
    
       }
    }
    

    Whenever you are done with showing the first activity simple set the shared prefs boolean flag to false:

    prefs.getEditor.setBoolean("firstStart", false).commit();
    

提交回复
热议问题