Re-Launch of Activity on Home Button, But...Only the First Time

Re-launch of Activity on Home button, but...only the first time

Welcome to the ever-growing list of users who have been bitten by this one.

This is a well-known and long-standing Android bug. in the way applications get launched the first time from the installer, web-browser and via IDE (IntelliJ, Eclipse, etc.). See these issues filed long ago related to the problem:

http://code.google.com/p/android/issues/detail?id=2373

http://code.google.com/p/android/issues/detail?id=26658

It is still broken and you cannot prevent this from happening. The only thing you can do is to detect when Android has launched a second instance of your root activity into an existing task. You can do this by putting this code in onCreate() of your root activity:

if (!isTaskRoot()) {
// Android launched another instance of the root activity into an existing task
// so just quietly finish and go away, dropping the user back into the activity
// at the top of the stack (ie: the last state of this task)
finish();
return;
}

How to restore previous activity after pressed home button and return to the app

The default Android behaviour when you press the home button is that the current activity is pushed onto the stack, unless you called finish() in your activity. Whenever you open your application it will launch the activity that's on top of your stack.

I'ts based on your system memory. If you don't have enough memory to keep your activity backgrounded then Android will kill your activity and its state to recoup some memory. I'm thinking that perhaps your phone (or emulated device) is running short of memory.

Android moving back to first activity on button click

button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(D.this, A.class));
}
});

Declare A in your manifest with the android:launchMode="singleTask". This way, when you call startActivity() from your other activies, and A is already running, it will just bring it to the front. Otherwise it'll launch a new instance.

How to return to the latest launched activity when re-launching application after pressing HOME?

Oh, I think I've found the answer.

Because I was launching the app by using IntelliJ, it seems it launches the application in a different way than a user, clicking a home screen widget, would launch. It's explained in the answer to another SO question:

This is due to the intents being used to start the app being different. Eclipse starts an app using an intent with no action and no category. The Launcher starts an app using an intent with android.intent.action.MAIN action and android.intent.category.LAUNCHER category. The installer starts an app with the android.intent.action.MAIN action and no category.

Ref: App always starts fresh from root activity instead of resuming background state (Known Bug)

So I've manually killed the application in the phone, and relaunched it again from the home screen widget. Then opened the GameActivity, and pressed HOME. Now, when relaunching it the GameActivity is still visible and keeping its UI state as it was when I left it.

And I guess that the reason why a new instance of the activity was created when pressing the shortcut before was due to a different Intent being used for starting the activity.

Activity be killed when press HOME only in release mode

Issue has been fixed by using FLAG_ACTIVITY_NEW_TASK when navigate from Splash to Main activity. I just wonder why it can work in debug mode (Reproduce install from apk like release mode). Here is my workable code for anyone has same issues:

 Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
finish();
startActivity(intent);

Update: many thanks to David Wasser, so the right answer should like this:

// SplashActivity
if(!isTaskRoot()) {
finish();
return;
}

Reference: Re-launch of Activity on Home button, but...only the first time



Related Topics



Leave a reply



Submit