After notification button return to original activity

烈酒焚心 提交于 2019-12-11 13:27:28

问题


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!


回答1:


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);
}



回答2:


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




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/33526086/after-notification-button-return-to-original-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!