I'm setting up Notifications in my app and I've noticed whilst testing, after clicking a new notifications (in this case the notification loads a blog details page), I have many instances of the blog details activity running (pressing back it shows each activity with the previously loaded blogs).

Is it possible in my Receiver class, so look if there is any instance of ActivityBlog already running, and if there all .finish() them all so there is only ever once instance running?

I found this but I couldn't work out a way to do it from that.

有帮助吗?

解决方案

You should study activity launch modes http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

Use android:launchMode="singleTop" in element in manifest file. You will get callback in onNewIntent() if an instance of activity is already up. Stack of your activities will be automatically updated and there wont be any need of killing activities which is consumes time and resources. This i believe is the recommended approach.

其他提示

    Intent z = new Intent(Projects_Accel.this,MainActivity.class);
    z.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
    Intent.FLAG_ACTIVITY_CLEAR_TASK |
    Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(z);

use this for kill all activity

Do this way

Intent intent = new Intent(this, ActivityBlog.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
activity.startActivity(mainIntent);

NOTE:

You need to put android-support.jar in libs folder

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top