Modify activity after clicking on a notification
I have a normal notification system that looks like this:
Notification notification new Notification(
R.drawable.alerts_notification,
alertTitle,
System.currentTimeMillis());
Intent intent = new Intent(mContext, MyActivity.class);
intent.setAction(MyActivity.ONE_ACTION);
PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
mNoti开发者_如何学CfMan.notify(ID, notification);
Notice that I'm using ONE_ACTION
as the action of the intent. What I do is verify the action on the activity and select one of the tabs (it's a TabActivity
).
All that works fine if the activity is closed, because the Intent
will open the activity and then I will decide what to do depending on the action in the Intent
. But, if the activity is already opened, it launches a new activity. On the other hand, if I add the flag Intent.FLAG_ACTIVITY_SINGLE_TOP
, the activity is not launched twice but I can't the tab is not chosen either.
So, how can choose a tab by clicking on the notification?
Have your intent open your tab activity and put an extra in it denoting the tab. When you detect action resume get a handle on your tab controller and change the tab through code.
OK, I found how to do it... it seems I hadn misread the documentation. What I did was:
- Add this to the activity in the
AndroidManifest.xml
file:android:launchMode="singleInstance"
- Overwrite the
onNewIntent(Intent intent)
method on the activity and put there all the logic to select the tabs etc. - Launch the intent with the flag
Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
@schwiz, thanks for your answer though.
精彩评论