简体   繁体   中英

Android:Splash screen only for the first time

我需要创建一个闪屏,它应该只在第一次安装和启动应用程序时显示,从第二次启动应用程序时它应该显示主要活动?我怎样才能实现它?

Okey looking at your problem you can do following..

First of all declare object of SharedPreference and on String which will we use later.

SharedPreferences loginPreference;
String MY_PREF = "my_pref";

Now in onCreate of your SplashActivity, do something like this.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // initialize SharePreference
    loginPreference = getSharedPreferences(MY_PREF, Context.MODE_PRIVATE);

    // this condition will do the trick.
    if(loginPreference.getString("tag", "notok").equals("notok")){

        // add tag in SharedPreference here..
        Editor edit = loginPreference.edit();
        edit.putString("tag", "ok");
        edit.commit();

        // your logic of splash will go here.
        setContentView(R.layout.splash);

    }else if(loginPreference.getString("tag", null).equals("ok")){
        Intent i = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(i);
        finish();
    }
}

Happy Coding..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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