문제

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