Mihir Shah
Mihir Shah

Reputation: 1809

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.

Upvotes: 7

Views: 4829

Answers (8)

Saleho
Saleho

Reputation: 1

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.

Upvotes: 0

Codeversed
Codeversed

Reputation: 9473

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

Upvotes: 0

Simo
Simo

Reputation: 345

Have you tried starting a thread in onBackPressed and sleep for a specific time after which it calls finish(),or else stop the thread (from calling finish), and gain back the same activity.

Upvotes: 0

David Wasser
David Wasser

Reputation: 95543

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

Upvotes: 10

Mohit marwal
Mohit marwal

Reputation: 251

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.

Upvotes: -2

Helal Khan
Helal Khan

Reputation: 887

You can put android:clearTaskOnLaunch="true" in your manifest for activity A to have the launcher always go to that activity.

Upvotes: 0

ankita gahoi
ankita gahoi

Reputation: 1562

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(i.e. 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.

Upvotes: -2

Chirag
Chirag

Reputation: 56925

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.

Upvotes: -1

Related Questions