Activity not being reused
I have an odd issue with my application.
I start the application and the activity shows ok.
I then press the home key so that the activity goes into the background. I can see the onPause()
method being called.
The application's service then creates a notification which shows on the status bar.
I then click on the notification and the activity is shown and I see that the onResum开发者_JAVA百科e()
method is called.
I then press the home key and the activity goes into the background. I can see the onPause()
method being called.
If I now start the application by clicking on the applications icon I see that a new instance of the activity is created rather than using the paused instance.
If I press the home key again the new activity goes into the background.
Starting the application by clicking on the applications icon I see another new instance of the activity is created.
Pressing the back button at the point destroys each activity in return.
What I want to happen is that a single instance of the activity be used.
Any ideas?
Just use the same intent filters as android uses when launches the app:
final Intent notificationIntent = new Intent(context, MessageListActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
As the intent you created to open your activity from notification bar is the same as android used for launching your app, the previously opened activity will be shown instead of creating a new one.
You should look at the different launch modes for Android activities. this should help. Launch modes can be set in the androidmanifest.xml file. I think your solution would be to use the 'singleTop' launch mode.
精彩评论