Force application to restart on first activity

2019-01-02 22:21发布

For an unknown reason, I can't get my application leaving properly so that when I push the home button and the app icon again, I resume where I was in the app. I would like to force the application to restart on the first Activity.

I suppose this has something to do with onDestroy() or maybe onPause() but I don't know what to do.

标签: android
11条回答
ら.Afraid
2楼-- · 2019-01-02 22:59

You can use Jake Wharton's lib ProcessPhoenix in order to restart your application process.

查看更多
forever°为你锁心
3楼-- · 2019-01-02 23:01

if you want to always start at the root you want to set android:clearTaskOnLaunch to true on your root activity

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-02 23:01

Does FLAG_ACTIVITY_CLEAR_TOP do what you need to do?

Intent i = new Intent(getBaseContext(), YourActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
查看更多
女痞
5楼-- · 2019-01-02 23:02
android:clearTaskOnLaunch="true"
android:launchMode="singleTask"

Use this property in manifest file in starting class (first activity).

查看更多
祖国的老花朵
6楼-- · 2019-01-02 23:02

this worked for me:

Intent mStartActivity = new Intent(ctx.getApplicationContext(), ActivityMain.class);


    mStartActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    mStartActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        mStartActivity.addFlags(0x8000); // equal to Intent.FLAG_ACTIVITY_CLEAR_TASK

   int mPendingIntentId = 123456;


    PendingIntent mPendingIntent = PendingIntent.getActivity(ctx, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, mPendingIntent);

    ActivityManager manager =  (ActivityManager) ctx.getApplicationContext().getSystemService(ctx.getApplicationContext().ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> activityes = ((ActivityManager)manager).getRunningAppProcesses();




    ((Activity)ctx).moveTaskToBack(true);
    manager.killBackgroundProcesses("com.yourpackagename");
    System.exit(0);
查看更多
登录 后发表回答