如何让应用程序在第一次启动时打开一个不同的活动,从那时开始另一个活动?

时间:2016-03-20 15:28:10

标签: java android xml

假设有两个活动A1和A2。 A1就像一个登录页面。 A2是主页。 如何在用户第一次启动应用程序时显示A1,但一旦登录,A2应该是用户在重新启动应用程序后首先看到的屏幕?

2 个答案:

答案 0 :(得分:1)

使用preferenceManager设置标志...

final String FIRST_TIME_KEY = "com.example.app.MainActivity.firstTimeKey";
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstTime = sp.getBoolean(FIRST_TIME_KEY, false);
if(isFirstTime) {
    SharedPreferences.Editor edit = sp.edit();
    edit.putBoolean(FIRST_TIME_KEY, true);
    edit.apply();

    //Start the frist time only activity
} else {
    //Start the normal regular activity
}

答案 1 :(得分:0)

你应该有一个固定的启动活动.. 那么你可以做什么,当活动启动后显示图像1或2秒后,你可以跳转到任何活动..第一次应用程序打开,你可以选择一个活动,然后当你再次打开应用程序时它将显示另一个活动..代码

 public class class_name extends AppCompatActivity {
 public static final String MyPREFERENCES2 = "MyPrefs" ;
 SharedPreferences sharedpreferences2;
 public boolean isFirstRun;

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


    new Timer().schedule(new TimerTask() {
        public void run() {
            checkFirstRun();

        }
    }, 3000);




}
   public void checkFirstRun() {
    System.out.println("its in check first run");
    isFirstRun = getSharedPreferences("PREFERENCE2",  MODE_PRIVATE).getBoolean("isFirstRun", true);
    if (isFirstRun){
       startActivity(new Intent(class_name.this, new_activity1.class));

        getSharedPreferences("PREFERENCE2", MODE_PRIVATE)
                .edit()
                .putBoolean("isFirstRun", false)
                .commit();

    }
    else{
         startActivity(new Intent(class_name.this, new_activity2.class));

        }
     }
}