After notification button return to original activ

2019-08-12 17:18发布

I wrote an app, that brings up a notification every certain time via an alarmIntent. The user clicks on the notification and gets into an activity. After doing something in this activity he should press a button to return to the original app he was in (like a browser, or the homescreen, etc.), before clicking on the notification. My problem is, that the user always returns to the main activity of my application. How can I change that?

Thank you!

4条回答
Explosion°爆炸
2楼-- · 2019-08-12 17:40

try adding this lines in your Activity

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    finish();
    System.exit(0);
}
查看更多
老娘就宠你
3楼-- · 2019-08-12 17:52

There are two types of Activity started by Notification : regular & special. Regular Activity always return to Launcher or your previous activity. Special Activity will return to your Application.

As your description, you should use Regular Activity and get PendingIntent by TaskStackBuilder, it will create an artificial back stack for notification activity.

for example :

PendingIntent resultPendingIntent =
    stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

And this is the document, you can find detail here. http://developer.android.com/guide/topics/ui/notifiers/notifications.html

查看更多
等我变得足够好
4楼-- · 2019-08-12 18:00

Try Activity.finish() inside the button click listener function. This is similar to pressing the back button.

查看更多
我只想做你的唯一
5楼-- · 2019-08-12 18:01

So I found the solution finaly: you need to add the Intent.FLAG_ACTIVITY_MULTIPLE_TASKS flag to the intent as explained in that question: Go back to previous screen on backbutton pressed after responding to notification

查看更多
登录 后发表回答