Mobile phone Audio and Video 1 -- SplashActivity, 1 -- splashactivity
/*** 1. set the started Activity to singleton mode */public class SplashActivity extends Activity {/*** whether the home page has been started */private boolean isStartMain = false; private static final String TAG = SplashActivity. class. getSimpleName (); private Handler handler = new Handler (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_splash); handler. postDe Layed (new Runnable () {@ Override public void run () {// This method is executed in the main thread Log. e (TAG, "Name of the current Thread:" + Thread. currentThread (). getName (); startMainActivity () ;}}, 3000) ;}/ *** start the home page */private void startMainActivity () {if (! IsStartMain) {isStartMain = true; Intent intent = new Intent (this, MainActivity. class); startActivity (intent); finish (); // close the current page }}@ Override protected void onDestroy () {// isStartMain = true; handler. removeCallbacksAndMessages (null); // remove all the reconciliation messages in the message queue from super. onDestroy () ;}@ Override public boolean onTouchEvent (MotionEvent event) {startMainActivity (); return super. onTouchEvent (event );}}
1. The thread where the Handler is located is new and the Runnable is executed.
2. There is a bug where the Activity is started multiple times. How can this problem be solved?
Method 1. Set the Activity to start to be a singleton only.
In the function list file android: launchMode = "singleTask"
Method 2. control from the source and execute the Code only once
Private boolean isStartMain = false;/*** jump to the homepage and close the current page */private void startMainActivity () {if (! IsStartMain) {isStartMain = true; Intent intent = new Intent (this, MainActivity. class); startActivity (intent); // close the current page finish ();}}