3

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.

4

2 回答 2

2

嘿,试试这个它对我有用,我们必须采用一个布尔标志,所以它会处理这个问题,当你在加载初始屏幕期间按下后退按钮时,它将停止该活动,因此自动重新加载问题将解决希望它对你有帮助。

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);

    }

}
于 2012-06-02T07:11:52.873 回答
0

这是两个应用程序实例的已知 Android 问题。该问题已在此处此处正式跟踪。

解决方法在这里

于 2010-11-08T20:33:10.260 回答