@@ -153,7 +153,7 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo
153153 JSONObject startOptions = data .getJSONObject (1 );
154154 this .startEngineWithScript (scriptBody , startOptions , callbackContext );
155155 } else if (action .equals ("reset" )) {
156- this .setReset (callbackContext );
156+ this .setReset ();
157157 } else {
158158 Log .e (LOGTAG , "Invalid action: " + action );
159159 return false ;
@@ -370,12 +370,12 @@ private boolean isEmptyNodeModules(){
370370 return !nodejsModulesFolder .exists ();
371371 }
372372
373- private void setReset (CallbackContext callbackContext ) {
373+ private void setReset () {
374374 SharedPreferences prefs = context .getSharedPreferences (SHARED_PREFS , Context .MODE_PRIVATE );
375375 SharedPreferences .Editor editor = prefs .edit ();
376376 editor .putBoolean (FORCE_RESET , true );
377377 editor .commit ();
378- sendResult ( true , "Reset done." , callbackContext );
378+ doColdRestart ( );
379379 }
380380
381381 private void clearReset () {
@@ -603,4 +603,54 @@ private static boolean getOptionRedirectOutputToLogcat(final JSONObject startOpt
603603 }
604604 return result ;
605605 }
606+
607+ /**
608+ * Performs a full cold app restart - restarts application
609+ * https://stackoverflow.com/a/22345538/777265
610+ */
611+ protected void doColdRestart () {
612+ String baseError = "Unable to cold restart application: " ;
613+ try {
614+ logInfo ("Cold restarting application" );
615+ Context c = applicationContext ;
616+ //check if the context is given
617+ if (c != null ) {
618+ //fetch the packagemanager so we can get the default launch activity
619+ // (you can replace this intent with any other activity if you want
620+ PackageManager pm = c .getPackageManager ();
621+ //check if we got the PackageManager
622+ if (pm != null ) {
623+ //create the intent with the default start activity for your application
624+ Intent mStartActivity = pm .getLaunchIntentForPackage (
625+ c .getPackageName ()
626+ );
627+ if (mStartActivity != null ) {
628+ //mStartActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
629+ //create a pending intent so the application is restarted after System.exit(0) was called.
630+ // We use an AlarmManager to call this intent in 100ms
631+ // int mPendingIntentId = 223344;
632+ // PendingIntent mPendingIntent = PendingIntent
633+ // .getActivity(c, mPendingIntentId, mStartActivity,
634+ // PendingIntent.FLAG_CANCEL_CURRENT);
635+ // AlarmManager mgr = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
636+ // mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
637+ mStartActivity .addFlags (Intent .FLAG_ACTIVITY_CLEAR_TASK );
638+ c .getApplicationContext ().startActivity (mStartActivity );
639+
640+ Log .i (TAG ,"Killing application for cold restart" );
641+ //kill the application
642+ System .exit (0 );
643+ } else {
644+ Log .e (LOGTAG , baseError + "StartActivity is null" );
645+ }
646+ } else {
647+ Log .e (LOGTAG , baseError + "PackageManager is null" );
648+ }
649+ } else {
650+ Log .e (LOGTAG , baseError + "Context is null" );
651+ }
652+ } catch (Exception ex ) {
653+ ex .printStackTrace ();
654+ }
655+ }
606656}
0 commit comments