简体   繁体   中英

ongoing notification opening existing Activity

I want to show a notification while a timer is running and when the user clicks the notifiaction it opens the timer activity.

        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intent = new Intent(this, Activity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

The problem right now is that when I start the timer and press home and then click the notification it opens the Activity with the running timer.

But when I start the timer, then open another activity (via ActionBar.NAVIGATION_MODE_LIST ), then press home and click the notification it opens a new Activity (empty).

I thought addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); would help (thats what I used to navigate between Activities)

im using android:launchMode="singleTop" and android:configChanges="orientation|screenSize"

for every Activity.

I wouldn't rely on your activity persisting and it is unnecessary

Store the timer start time into sharedpreferences and then simply load up the start time when the activity is recreated.

If something is meant to happen after a certain time, like a countdown time, then you'll need to set up an Alarm and handle it that way.

Remember that activities are just UI. Don't trust an activity to be doing anything when the user isn't looking at it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM