繁体   English   中英

该应用程序如何在首次启动时打开一个不同的活动,然后又打开另一个活动?

[英]How to have the app open a different activity for the first time it is launched and a different one from then on?

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

使用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或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));

        }
     }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM