Restarting app programmatically

Hi. I want to restart my app from startup and after googling some snippets I found code below. It gives however the Exception error. Until now I restart my program by emptying all arrays and images and run simply setup() again, but I would like to do it the proper way. Any suggestion?

import android.app.Activity; 
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.Intent;
import android.content.Context;
import android.app.AlarmManager;
import android.app.PendingIntent;

Activity act;
Context contxt;

void setup() {
  background(0, 0, 255);
  textSize(45);
  text("Hello", width/2, height/2);
  println("setup()_entry");
  act = this.getActivity();
  contxt = act.getApplicationContext();
}

void draw() {
}  

void mousePressed() {
  try {
    restart(contxt, 100);
  }
   catch (Exception e) {
    println("Restart error");
  }
}  

void restart(Context context, int delay) { 
  if (delay == 0) { 
    delay = 1;
  } 
  println("restart()_entry with context \""+context+"\"");
  Intent restartIntent = context.getPackageManager() .getLaunchIntentForPackage(context.getPackageName() ); 
  PendingIntent intent = PendingIntent.getActivity( context, 0, restartIntent, Intent.FLAG_ACTIVITY_CLEAR_TOP); 
  AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
  manager.set(AlarmManager.RTC, System.currentTimeMillis() + delay, intent); 
  System.exit(2);
}

Hope this help!

void mousePressed() {
       //get parent activity
       Activity current = getActivity();
       //start a same new one
       current.startActivity(new Intent(getContext().getApplicationContext(),current.getClass()));
       //finish current 
       current.finish();
}
1 Like

@noel ===
your code seems ok
what exception ?
PS: why using System.exit()? - Generally with android “finish” is better


Thanks @technew Both codes work, but yours better. Normally when I start an app it has a noticeable blank screen for some milliseconds before it actually shows up. When I use the code I posted, even with a delay of one millisecond it has the same behaviour. With your code however it restarts in a mini blink. (I couldn’t even believe it. I printed a teller in the draw() to verify that it really restarted.:slight_smile:)
I would like to know what is happening in the background. I mean, both codes are using activity.finish(). When calling finish() on an activity I think the method onDestroy(); is called.
What’s the difference?

@akenaton Thanks, but this time I already had figured this out. Slowly I am making progressâ˜ș.

Well, if you dive into the android source code, it’s far more complex than you might think.

I see the android finish() method, but both codes use these. (first code needs act.finish() not System.exit() to work)
First code uses FLAG_ACTIVITY_CLEAR_TOP, and I was reading about Android Task and Back Stack Review where is said:

Note: For FLAG_ACTIVITY_CLEAR_TOP , if the launchMode is not defined in the AndroidManifest or set as “ standard” for the Activity then the Activity along with its top is popped and a new instance of that Activity is placed on the top. So, onNewIntent method is not called.
This is undesirable, most of the time we would want to reuse the Activity and refresh it’s view states like, the data in the list, when it comes to the top of the back stack, rather than destroying and then recreating it.
In order to achieve this, we define the launchMode of the given Activity as singleTop and call the startActivity() with flag FLAG_ACTIVITY_CLEAR_TOP

Maybe that’s why. But the impression that I have is that your code " skips" someting, and I can’t figure out what.