0

I'm launching a MainActivty from a BroadcastReceiver when Android starts:

Intent miIntent = new Intent(this, MainActivity.class);
miIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
this.startActivity(miIntent);

This works fine; the MainActivity is launched. The problem is when I press the home button on MainActivity and then I open the application again. It doesn't resume the app, but instead it creates a new Activty. If I press a button finish (the MainActivity contains a button to finish()) MainActivity appears again, because the last doesn't close. Help me please, I dont know what to do.

I think the problem is in the flags, because if I open the app again, it works fine (without booting from broadcastreceiver).

Thanks!!

AndyG
  • 39,700
  • 8
  • 109
  • 143

1 Answers1

1

You can try adding the FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_CLEAR_TOP flags to your intent.

mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP |     Intent.FLAG_ACTIVITY_NEW_TASK);

Flag Activity Clear Top: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

Flag Activity Clear Task: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK

OR

You can try adding the android:launchMode="singleTop" to your MainActivity in the manifest, this says that only one instance of the activity will run. So if another instance of it does exist it will resume it instead of replacing it with a new one.

Good Luck!

DejanRistic
  • 2,039
  • 18
  • 13