Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single Instance of Activity

My application has three activities say A -> B-> C.

Activity A is called from another activity through startActivityForResult(). Activity B and C are also called similarly. I have to call activity A from notifications bar also (if there is some specific notification).

Now, if currently I am in activity B or C, and I click on Notification bar, and call the activity A, the app goes to Activity A only and data entered through activites B or C do not persist.

I don't want such behavior. I want that if I click on Notification, it should redirect to current screen only. Can some one help. (I mentioned activity:launchMode as SingleTask).

like image 385
Sachchidanand Avatar asked Jul 15 '11 11:07

Sachchidanand


1 Answers

Using android:launchMode="singleTask" is probably the best approach, since it won't recreate the activity if it's already running. Just add it to the activity in your AndroidManifest.xml, and you should be all set.

<activity
    android:name=".MyActivity"
    android:label="@string/app_name"
    android:launchMode="singleTask"
    >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
 </activity>

Here's another question that might be useful: Android singleTask or singleInstance launch mode?

like image 98
derekerdmann Avatar answered Oct 17 '22 10:10

derekerdmann