开发者

Modify activity after clicking on a notification

开发者 https://www.devze.com 2023-01-17 14:10 出处:网络
I have a normal notification system that looks like this: Notification notification new Notification( R.drawable.alerts_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:

  1. Add this to the activity in the AndroidManifest.xml file: android:launchMode="singleInstance"
  2. Overwrite the onNewIntent(Intent intent) method on the activity and put there all the logic to select the tabs etc.
  3. Launch the intent with the flag Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT

@schwiz, thanks for your answer though.

0

精彩评论

暂无评论...
验证码 换一张
取 消