简体   繁体   中英

Application not open main activity when it was killed after pressing home button in android

In my Application, activity A is launcher activity, from A it called B and from B it called C, I have such more than 5 activities. In C when I press home button, and again open my app it open C, that is fine in my case. But after pressing home button in C, when it idle for some time and application is killed, after that when I open my app it opens C. But I want to open main launcher activity that time. How can I do this?

A > B > C > HOME button > idle for some time > application killed > open app > C.

In this case I want to open main activity A instead of C.

You will need to detect that Android has killed your process and then after that the user has returned to the app (causing Android to create a new process). I've described how to do this in numerous answers:

https://stackoverflow.com/a/29802601/769265

https://stackoverflow.com/a/27276077/769265

https://stackoverflow.com/a/11249090/769265

您可以将android:clearTaskOnLaunch="true"放在活动 A 的清单中,以使启动器始终转到该活动。

您是否尝试过在 onBackPressed 中启动一个线程并在特定时间内休眠,然后调用finish(),或者停止该线程(从调用finish),并获得相同的活动。

I know this is an old post and as David Wasser's answer above has been the best solutions in the past... there is now a better way of doing things.

The better way to do all of this using ProcessLifecycleOwner , which is designed for just this. In short and right from the JavaDoc:

"Class that provides lifecycle for the whole application process."

If your application is killed, the Application lifecycle onDestroy would be called, which allows you to creatively track this how you wish. It could be as simple as setting a SharedPreference flag that you can check when the application restarts incorrectly.

Please note, you need to add appropriate android.arch dependencies to take advantage of the new LifecycleObserver , ProcessLifecycleOwner , etc.

Below is an example of how you can handle each LifecycleEvent for the whole Application.

public class MyApplication extends Application implements LifecycleObserver {

    @Override public void onCreate() {
        super.onCreate();
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreate(){
        // ... Awesome stuff
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onStart(){
        // ... Awesome stuff
    }   

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume(){
        // ... Awesome stuff
    }   

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause(){
        // ... Awesome stuff
    }   

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop(){
        // ... Awesome stuff
    }   

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestory(){
        // ... Awesome stuff
    }   

    @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
    public void onAny(){
        // ... Awesome stuff
    }
}

Here is a nice article where I learned about this along with Kotlin examples and a few more tips beyond this post: Background and Foreground events with Android Architecture Components

App is being killed and activity is being killed are two different things. To test that your activity is killed (without kill the app) activate devloper mode and set kill activity when leave.

Pressing the Home switches you from the app to the home screen, whilst leaving your app running in the background.

Except that when your phone is running low on resources like memory it will start to close apps that are running in the background, so that your phone has enough resources for what you're trying to do now. Games are often amongst the first apps the phone will "kill" to save resources as they often use a lot more memory and CPU than other apps. This is why sometimes your game is stil lrunning paused, and sometimes Android has closed it for you.

on pressing home ,u have not finished activity it still exists in stack.only it goes to pause so use this.

if you are calling activity B from an activity A.and C from B

A->B

use startactivityforresult from A

and again B->C

use startactivityforresult from B

and when you want to exit from C then setResult(ie RESULT_OK) and finish C.and in OnActivityResult() in B,check if resultcode == RESULT_OK then again finish B and setresult(RESULT_OK) for A.same procedure will follow to finish A.

this will exit you from the application.and application will start from A not from C.

if your app is killed by the system i dont think it will start from c. If you are killing it through an task killer app then its a mistake. Force stop it from the app settings and then check.However if killed by task killer app and then from C if you get back to B and it is crashing then check for result code. if resultcode != RESULT_OK then you can handle your code here and save app from crash. if you have not started you activity for result then finish B and A before launching B and c.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM