7

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.

Mihir Shah
  • 1,799
  • 3
  • 29
  • 49
  • 4
    and how do you assume that the app is getting killed? It does not happen just because your app is idle for sometime. If it has started from C means it was never killed. An application is automatically killed by the device only when the device is under load and dearth of memory – Vicky Kapadia Sep 11 '12 at 06:09
  • 1
    In ddms logcat, it is showing process has died. – Mihir Shah Sep 11 '12 at 06:56
  • 1
    @VickyKapadia Android can and will kill background processes at any time. Especially on low-end devices and devices with weak batteries, Android will aggressively kill off background processes even if it doesn't need the memory. – David Wasser Apr 20 '17 at 09:39
  • You will need to detect that Android has killed your process. To do this see my answer here: http://stackoverflow.com/a/29802601/769265 – David Wasser Apr 20 '17 at 09:52

8 Answers8

10

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

Community
  • 1
  • 1
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • David, in the first answer you suggest to initialize a boolean in first activity's `onCreate()`. Ok. Android kills process. User launches app again. All the activities which were previously in backstack would be created and put in backstack again, meaning that `onCreate()` of `FirstActivity` will also be called, which will result that boolean to become `true`. So, whenever you check that boolean value it will always be `true`. Am I missing something? – azizbekian Apr 20 '17 at 12:02
  • 2
    No, after user launches the app again, only activity that was last open is put on the stack (in OP's case Activity C) and its `onCreate()` is called. Then user navigates back to B -> `onCreate()` of B is called, and so on. I used David's solution in my app it works! Brilliant solution, thanks! – Damnum Apr 20 '17 at 12:51
  • 2
    @azizbekian As @Damnum says, when Android creates a new OS process, it only creates the topmost `Activity` from the stack. The other activities are NOT recreated until the user BACKs into them. – David Wasser Apr 20 '17 at 13:34
0

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

Helal Khan
  • 867
  • 3
  • 10
  • 25
  • I know that, but I want to open A only when application is killed, not want to open always A after pressing home button. – Mihir Shah Sep 11 '12 at 06:01
0

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.

Simo
  • 345
  • 4
  • 12
0

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

Codeversed
  • 9,287
  • 3
  • 43
  • 42
0

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.

Saleho
  • 1
-1

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.

Chirag
  • 56,621
  • 29
  • 151
  • 198
-2

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.

ankita gahoi
  • 1,532
  • 2
  • 15
  • 28
  • This doesn't actually adress the problem that OP has. If Android kills his process, when the user returns to the app it will still start a new process and show `C`, This is not what OP wants. – David Wasser Apr 20 '17 at 09:50
-2

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.

Mohit marwal
  • 251
  • 5
  • 12
  • 1
    This is wrong. If Android kills a background process, it remembers the state of the task stack and which activities were opened. When the user returns to that task, Android creates a new OS process and restores the state of the **top `Activity` in the stack**. In OP's case, It will create a new instance of `C`, since that is what was the top `Activity` in the stack when the user put the task in the background. – David Wasser Apr 20 '17 at 09:37