How do I programmatically “restart” an Android app?

后端 未结 26 1978
小鲜肉
小鲜肉 2020-11-22 12:42

Firstly, I know that one should not really kill/restart an application on Android. In my use case, I want to factory-reset my application in a specific case where a server s

相关标签:
26条回答
  • 2020-11-22 13:09

    Here is an example to restart your app in a generic way by using the PackageManager:

    Intent i = getBaseContext().getPackageManager()
                 .getLaunchIntentForPackage( getBaseContext().getPackageName() );
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    
    0 讨论(0)
  • 2020-11-22 13:09

    Use:

    navigateUpTo(new Intent(this, MainActivity.class));
    

    It works starting from API level 16 (4.1), I believe.

    0 讨论(0)
  • 2020-11-22 13:10

    You can restart your current activity like this:

    Fragment :

    activity?.recreate()
    

    Activity :

    recreate()
    
    0 讨论(0)
  • 2020-11-22 13:11

    The application I'm working on has to give the user the possibility to choose which fragments to display (fragments are dynamically changed at run-time). The best solution for me was to restart completely the application.

    So I tried plenty of solutions and none of them has worked for me, but this:

    final Intent mStartActivity = new Intent(SettingsActivity.this, Splash.class);
    final int mPendingIntentId = 123456;
    final PendingIntent mPendingIntent = PendingIntent.getActivity(SettingsActivity.this, mPendingIntentId, mStartActivity,
                        PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager mgr = (AlarmManager) SettingsActivity.this.getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
    this.finishAffinity(); //notice here
    Runtime.getRuntime().exit(0); //notice here
    

    Hoping that is going to help someone else!

    0 讨论(0)
  • 2020-11-22 13:12

    I have slightly modified Ilya_Gazman answer to use new APIs (IntentCompat is deprecated starting API 26). Runtime.getRuntime().exit(0) seems to be better than System.exit(0).

     public static void triggerRebirth(Context context) {
        PackageManager packageManager = context.getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(context.getPackageName());
        ComponentName componentName = intent.getComponent();
        Intent mainIntent = Intent.makeRestartActivityTask(componentName);
        context.startActivity(mainIntent);
        Runtime.getRuntime().exit(0);
    }
    
    0 讨论(0)
  • 2020-11-22 13:16

    The only code that did not trigger "Your app has closed unexpectedly" is as follows. It's also non-deprecated code that doesn't require an external library. It also doesn't require a timer.

    public static void triggerRebirth(Context context, Class myClass) {
        Intent intent = new Intent(context, myClass);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
        Runtime.getRuntime().exit(0);
    }
    
    0 讨论(0)
提交回复
热议问题