0

Good evening! I need to create an app which will always return after user minimizes it (like by clicking HOME button). I think I will have to add a Service with Timer, which will check if the Activity is Visible or invisible at the moment and return to app if invisible

if(!App.isActivityVisible()){
    Intent intent = App.getCurrentIntent();
    if(intent != null){
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}

But app creates a new activity, but I want to get the old activity...

But I think there is a simpler solution...

EDIT

So, I get the old activity, but when I pressed button back - my app finish, but I want to get previous activity.

I use

intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

EDIT

Yes, I found solution

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Shivam Kumar
  • 1,839
  • 1
  • 20
  • 30
  • Could you please confirm if you want to open the app again or do something else after the Home button is pressed? – Atul O Holic Feb 13 '14 at 15:05
  • If your activity exists, use http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT – 323go Feb 13 '14 at 15:13

2 Answers2

0

You cannot guarantee that there is even an Activity to recreate.

Android can destroy your Activities while they are stopped whether you want it to or not. You should design your Activities to properly handle this behavior. The default Activity lifecycle will give you the desired behavior until the system needs to destroy your Activity to free up system resources.

If you have state that you need to maintain when the Activity is destroyed and recreated, you should look at the Saving Persisted State documentation.

If you want to implement some sort of long running operation, you should look into implementing a Service.

Reading the documentation for Intent.FLAG_ACTIVITY_NEW_TASK will reveal why the code you posted doesn't do what you expect:

If set, this activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move to.

Not only are you not recreating an old Activity that was put in the background, but you are starting a new instance of that Activity on a completely new task stack.

Bryan Herbst
  • 62,910
  • 10
  • 119
  • 113
0

Your problem probably has something to do with the Intent flags used to launch the activity and the activity's launchMode in the manifest.

This might be useful:

How do you use Intent.FLAG_ACTIVITY_CLEAR_TOP to clear the Activity Stack?

Basically, although you can't control if the Activity will still be in the state that the user left it (Android may destroy it), you should be able to return to it as it was if Android has not destroyed it.

Also, understand that you may already be doing that but have code in onStart or onResume that makes it appear to be "restarting" even if Android has not destroyed it.

You should also be able to save state even if the Activity is destroyed, so that to the user it appears to be the way they left it even if Android destroys it.

Community
  • 1
  • 1
Jim
  • 9,902
  • 1
  • 23
  • 35