Android activity with screen restart and secret button

I have an Android application with a splash screen.

This screen saver preloads the data and, upon completion, launches the main action of the application and ends (through the call to finish ()).

This works well enough until the application is completely killed. Thus, I usually switch between different tasks, as usual: when I leave the application from auxiliary activity and return shortly after this auxiliary view is presented to me.

Now, when I leave this supporting activity and do some other things for a while, inevitably this application process is destroyed by the OS.

There are no problems so far. Now, I would expect Android to be unaware of preloading (if the data was not preloaded, it just takes longer or does not display some fonts, but Android cannot know that I am preloading somewhere) to restore sub-activity from the Bundle. However, splash screen activity starts.

So, I say that it’s good, then ... the activity of the splash screen is after the whole launch / main action. Now I have a mysterious secret.

When I click the back button on this newly loaded pop-up screen, I will be represented by the sub-activity that I left the application before it was killed. I really don't get it. Obviously, the Android DID saves the state of auxiliary activity (and its history stack) for rebooting, but instead of rebooting, he decided to launch the splash screen instead, with this auxiliary activity (I left the task before it was killed) one step back to the activity stack.

Why is this happening?

, , . , ( - ). , Android .. , ( ).

P.S. . , ..

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

    }

}
+2

Android . .

.

0

Source: https://habr.com/ru/post/1773670/


All Articles