Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn to activity without creating a new instance
    primarykey
    data
    text
    <p>I have a SplashScreenActivity which will run every 2 minutes if there is no touch detected on the MainActivity. If the 'Start' button is pressed on the SplashScreenActivity, it starts MainActivity.</p> <p>My problem is when the 'Start' button is pressed on the SplashScreenActivity, a new instance of MainActivity is created each time, thus loading up my libraries and initialization each time (in OnCreate()). This significantly slows down my application and lags when the button is pressed. I only want this to run <strong>once</strong> when the application is first started.</p> <p>I have tried using</p> <pre><code> new Handler().postDelayed(new Runnable() { /* * Showing splash screen with a timer. This will be useful when you * want to show case your app logo / company */ @Override public void run() { // This method will be executed once the timer is over // Start your app main activity Intent i = new Intent(SplashScreen.this, MainActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); // close this activity finish(); } }, SPLASH_TIME_OUT); </code></pre> <p>...when the Intent has started, but my libraries and initialization in OnCreate() in MainActivity are still being run again.</p> <p>When 'Start' button pressed in SplashScreenActivity, runs the following method:</p> <pre><code> public void startIntent(View v){ Intent i = new Intent(SplashScreen.this, MainActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); } </code></pre> <p>Any help?</p> <hr> <p>Currently have the lines (taken out setFlags):</p> <pre><code>Intent intent = new Intent(Email.this, MainActivity.class); startActivity(intent); </code></pre> <p>And still MainActivity's OnCreate() is being called each time the activity has started.</p> <hr> <p>So I've found out that if I set the following:</p> <pre><code>intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); </code></pre> <p>I can successfully return to my MainActivity without it creating a new instance.</p> <p>Click <a href="http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT" rel="nofollow">here</a> for more info.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload