Sunday, November 4, 2012

Start Activity on click notification makes click to back key more time to go on home.

Activity attributes work well when you always want the same behavior for an activity. But sometimes you need control over a specific activity launch. For those cases, use Intent flags. Intents are used to launch activities on Android. You can set flags that control the task that will contain the activity. Flags exist to create a new activity, use an existing activity, or bring an existing instance of an activity to the front.
For example, it's common to launch an activity when the user taps a notification. Often, apps will use the default intent flags, resulting in multiple copies of the same activity in the back stack. This forces the user to press back repeatedly while each instance of the activity is popped off the back stack. To fix this problem, set the flags Intent.FLAG_ACTIVITY_NEW_TASK and Intent.FLAG_ACTIVITY_CLEAR_TASK to switch to an existing instance of the activity and clear any other activities on top of it:
Intent intent = new Intent(context, TestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Note that in Android API version 11, the Intent class includes new static methods that create intents with the proper flags already set. Look here for an example.

No comments:

Post a Comment