Tibor
Tibor

Reputation: 589

I dont want more than one instance of my activity

My service has a notification with an intent to launch the associated activity, but I find sometimes that when pressing back in my activity it returns to a previously loaded activity, so it looks like there are sometimes where there are more than one copy of the activity in memory. How can I avoid this? I suppose I must bring to front in case exist or launch a new one but dont know exactly how. thanks.

Upvotes: 1

Views: 931

Answers (2)

gwvatieri
gwvatieri

Reputation: 5173

Add the flag Intent.FLAG_ACTIVITY_CLEAR_TOP to your intent

Upvotes: 0

Arslan Anwar
Arslan Anwar

Reputation: 18746

In you AndroidManifest.xml add android:launchMode="singleTask" with the as attribute with the desire activity you want to run only 1 instance.

e.g

<activity
        android:name=".MyActivity"
        android:launchMode="singleTask">

You can even use Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT flag with you activity intent. So that when activity is already running it will be brought to front instead of creating new.

Upvotes: 4

Related Questions