pinoyyid
pinoyyid

Reputation: 22296

How do I query the activities on the stack?

I have an activity that edits a contact item. As well as being invoked by navigation within the app, I have a home screen shortcut to the EditItem activity.

My problem is that potentially a user could be editing a contact, forget that he was doing so, and re-edit the contact via the shortcut, ie there could be two EditItem activities on the stack. If this happens, edits from one will clobber the other.

How do I detect and/or avoid this scenario?

Upvotes: 1

Views: 105

Answers (1)

tar
tar

Reputation: 1568

Just mark your edit activity as a "singleTask" in your AndroidManifest.xml file:

<activity
    android:name=".EditActivity"
    android:label="@string/app_name"
    android:launchMode="singleTask" />

You can read up on the launchMode mode in the Android Developer Guide. To quote it,

The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

For instance, I've used this mode for a map activity that could cause cycles in the activity stack. To avoid a user doing activity A -> B -> C -> B -> C and then having to press Back through all of them, I made activity C a "singleTask". That way the user only ever had to press Back once from activity B to reach activity A.

Upvotes: 1

Related Questions