Android Notification - How to bring the activity to front without recall the activity

梦想与她 提交于 2019-12-11 19:15:32

问题


In my app, in an activity(AndroidNotification.java) i am doing the following:

1) i am showing a notification when user press home button.

2) when user click the notification i show the app with the activity - AndroidNotification.java

3) i close the notification on notification bar when my app comes to front(that is in onResume method)

The below code working but the problem is the notification call the activity(AndroidNotification.java) again

my question is i do not want to relaunch the activity , the notification should bring the

activity to front from background, it should not call relaunch.

result of the current behavior is activity is called again so that whole functionality is worked again. that is the problem .

my code:

private NotificationManager mNotificationManager;
Notification notifyDetails;
......
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notifyDetails = new Notification(R.drawable.ic_launcher,
            "Notification, Click Me!", System.currentTimeMillis());

    Context context = AndroidNotification.this;
    CharSequence contentTitle = "Notification Test";
    CharSequence contentText = "Get back to Application on clicking me.";

    Intent notifyIntent = new Intent(context, AndroidNotification.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT );

    PendingIntent intent = PendingIntent.getActivity(
            AndroidNotification.this, 0, notifyIntent,0);

    // Set properties to notify object - its title, text
    notifyDetails.setLatestEventInfo(context, contentTitle,contentText, intent);

    // Notify user in status bar
    mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);

is anything need to add for the particular activity in AndroidManifest.xml

please help me.


回答1:


i have solved by adding the following in on create method

if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { 
    // Activity was brought to front and not created, 
    // Thus finishing this will get us to the last viewed activity 
    finish(); 
    return; 
}


来源:https://stackoverflow.com/questions/11743184/android-notification-how-to-bring-the-activity-to-front-without-recall-the-act

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