Android Does Not Reuse Main Activity And Starts Multiple Instances ( Behavior Only After Using Pendingintent )
Solution 1:
In the first case you comment, your activity is paused and has not been destroyed, so it moves from paused state to resumed state. You may check the activity lifecycle to learn more about it.
You should set
android:launchMode="singleInstance"
to your activity (in the manifest) and receive new intents inside:
- onCreate(...) in case the activity has been destroyed
- onNewIntent(...) in case the activity is paused/resumed
Hope it helps.
Solution 2:
If you only have a single Activity
, you should be able to do this in showReuseNotification()
:
IntentreuseIntent=newIntent(this, MainActivity.class);
reuseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntentreusePendingIntent= PendingIntent.getActivity(this, 2,
reuseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
This will bring an existing instance of your application to the foreground (if there is one), or start a new one (if there isn't already an active one).
Solution 3:
In the second case, your activity is still visible, but not in the foreground, because the notification bar uses the trick of stacking its own transparent activity on top of the second activity to make it appear as if a status bar was getting pulled down on top of the Activity.
And because of this special case, Android assigns a different priority to the activity underlying that foreground activity.
Let me look for a reference to what I'm saying. I'll be back soon.
Post a Comment for "Android Does Not Reuse Main Activity And Starts Multiple Instances ( Behavior Only After Using Pendingintent )"