How do I make a splash screen load once?

Deadly 提交于 2019-12-10 23:49:24

问题


So... I've developed a splash screen which is running successfully. How can I make it run once (and only once)? I'd like to build a registration screen but I only want it to appear for the user once.

Help!

Amani Swann

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.content.Intent;
import com.nfc.linkingmanager.R;

public class SplashScreen extends Activity {

private boolean mIsBackButtonPressed;
private static final int SPLASH_DURATION = 1000; 

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.splash_screen);

    Handler handler = new Handler();


    handler.postDelayed(new Runnable() {

        @Override
        public void run() {



            finish();

            if (!mIsBackButtonPressed) {

                Intent intent = new Intent(SplashScreen.this, NewCore.class);
                SplashScreen.this.startActivity(intent);
           }

        }

    }, SPLASH_DURATION);

}

 @Override
 public void onBackPressed() {
    mIsBackButtonPressed = true;
    super.onBackPressed();

}
}

回答1:


Use SharedPreferences to store the fact that the screen has already been displayed; upon start, you check this and if so, you replace the Activity by starting the next one and calling finish() for the splash screen immediately.




回答2:


You need to store a flag in shared preferences like this

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this );
finish();
if (prefs.getBoolean("SplashFlag", false)&&!mIsBackButtonPressed){

 Intent intent = new Intent(SplashScreen.this, NewCore.class);
                SplashScreen.this.startActivity(intent);
}else {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("SplashFlag", true); // value to store
//again there are values for int, long, string, float, boolean
editor.commit();//This is needed or the edits will not be put into the prefs file
}



回答3:


Use SharedPreferences where you could store a boolean for instance seenSplash which you could default false. Then use an if that checks if it's false and then shows the splash. After a user has seen the splashscreen get an Editor where you change that boolean to true.

Don't forget to commit() the change on the editor.




回答4:


in your register screen get login details and compare with values which you have saved in shared preferences. or hard-code like username is admin and password is admin. is both values are true just to enter splash screen otherwise redirect to other page whatever you like.




回答5:


There are two ways to do that

1st

finish();
SplashScreen.this.startActivity(intent);

2nd in Your Android Manifest where you declared your splash screen activity

add this

android:noHistory=true


来源:https://stackoverflow.com/questions/15479515/how-do-i-make-a-splash-screen-load-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!