Question

I have an Android app with a splash screen.

This splash screen pre-loads data and when finished starts the application's main activity and finishes (through a finish() call).

This works quite well as long as the application is not totally killed. So, I can usually switch back and forth between different tasks as usual: when I leave the app from a sub activity and return soon after I will be presented with this sub activity.

Now, when I leave this sub activity and do some other stuff for a while inevitably this application process is killed by the OS.

No problem so far. Now I would expect Android, being unaware of my preloading (if the data was not preloaded it would just take longer or not display some fonts, but Android cannot be aware of the fact that I am doing preloading somewhere), to restore the sub activity from a Bundle. However the splash screen activity is started.

So, I say, that's fine then... the splash screen activity is after all the launcher / main activity. Now, the actual mystery I have is as follows.

When I press the back-button from this newly loaded splash screen I will be presented with the sub activity I left the application from before it got killed. I really don't understand this. Apparently Android DID save the sub activity's state (and its history stack) to be reloaded but instead of reloading it chose to start the splash screen instead, with this sub activity (I left the task earlier before it got killed) one step back on the activity stack.

Why does this happen?

When the process is not killed I can switch back to where I left off. When it's killed I cannot (yet still have the whole earlier history of that app restored). I know that Android has to load state etc. in the latter case, but that shouldn't be a problem and is performed automatically by default (according to the docs).

P.S. I am not doing anything fancy. Default launch flags, no overwritten state restore methods etc.

Was it helpful?

Solution

Hey try this its works for me we have to take one Boolean Flag, so it will handle this problem and when you press back button during loading of splash screen it will stop that activity so automatic reloading problem will solve hope its helpful for you.

public class Main extends Activity {

    ImageView imageLogo;
    LinearLayout myLayout;
    private Thread splashTread;
    private boolean isBackPressed = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        splashTread = new Thread() {
            public void run() {
                try {
                    sleep(3000);
                    if (!isBackPressed) {

                        Intent myIntent = new Intent(
                                "src.SplashScreen.com.MENU");
                        startActivity(myIntent);

                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                finally {

                    finish();

                }
            }

        };

        splashTread.start();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            isBackPressed = true;
            finish();
        }
        return super.onKeyDown(keyCode, event);

    }

}

OTHER TIPS

This is a known Android issue with two application instances. The issue has been officially tracked here and here.

The workaround is here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top