How do I programmatically “restart” an Android app?

后端 未结 26 2000
小鲜肉
小鲜肉 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:16
    Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    
    0 讨论(0)
  • 2020-11-22 13:16

    Try using FLAG_ACTIVITY_CLEAR_TASK

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

    I had to add a Handler to delay the exit:

     mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 200, mPendingIntent);
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Runtime.getRuntime().exit(0);
                }
            }, 100);
    
    0 讨论(0)
  • 2020-11-22 13:17

    You can simply call:

    public static void triggerRebirth(Context context, Intent nextIntent) {
        Intent intent = new Intent(context, YourClass.class);
        intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(KEY_RESTART_INTENT, nextIntent);
        context.startActivity(intent);
        if (context instanceof Activity) {
          ((Activity) context).finish();
        }
    
        Runtime.getRuntime().exit(0);
    }
    

    Which is used in the ProcessPhoenix library


    As an alternative:

    Here's a bit improved version of @Oleg Koshkin answer.

    If you really want to restart your activity including a kill of the current process, try following code. Place it in a HelperClass or where you need it.

    public static void doRestart(Context c) {
            try {
                //check if the context is given
                if (c != null) {
                    //fetch the packagemanager so we can get the default launch activity 
                    // (you can replace this intent with any other activity if you want
                    PackageManager pm = c.getPackageManager();
                    //check if we got the PackageManager
                    if (pm != null) {
                        //create the intent with the default start activity for your application
                        Intent mStartActivity = pm.getLaunchIntentForPackage(
                                c.getPackageName()
                        );
                        if (mStartActivity != null) {
                            mStartActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            //create a pending intent so the application is restarted after System.exit(0) was called. 
                            // We use an AlarmManager to call this intent in 100ms
                            int mPendingIntentId = 223344;
                            PendingIntent mPendingIntent = PendingIntent
                                    .getActivity(c, mPendingIntentId, mStartActivity,
                                            PendingIntent.FLAG_CANCEL_CURRENT);
                            AlarmManager mgr = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
                            mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
                            //kill the application
                            System.exit(0);
                        } else {
                            Log.e(TAG, "Was not able to restart application, mStartActivity null");
                        }
                    } else {
                        Log.e(TAG, "Was not able to restart application, PM null");
                    }
                } else {
                    Log.e(TAG, "Was not able to restart application, Context null");
                }
            } catch (Exception ex) {
                Log.e(TAG, "Was not able to restart application");
            }
        }
    

    This will also reinitialize jni classes and all static instances.

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

    Directly start the initial screen with FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK.

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

    You can use startInstrumentation method of Activity. You need implement empty Instrumentation and pointed in manifest. After that you can call this method for restart your app. Like this:

    try {           
        InstrumentationInfo info = getPackageManager().queryInstrumentation(getPackageName(), 0).get(0);
        ComponentName component = new ComponentName(this, Class.forName(info.name));
        startInstrumentation(component, null, null);
    } catch (Throwable e) {             
        new RuntimeException("Failed restart with Instrumentation", e);
    }
    

    I get Instrumentation class name dynamically but you can hardcode it. Some like this:

    try {           
        startInstrumentation(new ComponentName(this, RebootInstrumentation.class), null, null); 
    } catch (Throwable e) {             
        new RuntimeException("Failed restart with Instrumentation", e);
    }
    

    Call startInstrumentation make reload of your app. Read description of this method. But it can be not safe if acting like kill app.

    0 讨论(0)
提交回复
热议问题