# Restarting app programmatically

**URL:** <https://discourse.processing.org/t/restarting-app-programmatically/14658>\
**Category:** Processing for Android\
**Created:** [October 13, 2019, 3:55pm UTC](https://discourse.processing.org/t/restarting-app-programmatically/14658 "2019-10-13T15:55:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 13, 2019, 3:55pm UTC](https://discourse.processing.org/t/restarting-app-programmatically/14658/1 "2019-10-13T15:55:18Z")

</div>

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?

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![technew](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/technew/32/6364_2.png) [@technew](https://discourse.processing.org/u/technew)\
**Post date:** [October 20, 2019, 12:10pm UTC](https://discourse.processing.org/t/restarting-app-programmatically/14658/2 "2019-10-20T12:10:08Z")

</div>

Hope this help!

```auto
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();
}

```

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [October 22, 2019, 3:01pm UTC](https://discourse.processing.org/t/restarting-app-programmatically/14658/3 "2019-10-22T15:01:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 22, 2019, 6:06pm UTC](https://discourse.processing.org/t/restarting-app-programmatically/14658/4 "2019-10-22T18:06:39Z")

</div>

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.🙂)  
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?

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 22, 2019, 6:09pm UTC](https://discourse.processing.org/t/restarting-app-programmatically/14658/5 "2019-10-22T18:09:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![technew](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/technew/32/6364_2.png) [@technew](https://discourse.processing.org/u/technew)\
**Post date:** [October 23, 2019, 4:50am UTC](https://discourse.processing.org/t/restarting-app-programmatically/14658/6 "2019-10-23T04:50:45Z")

</div>

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

 ![ssss](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/d896309c9bac29ddacfca5c5d311a1c5830af65c.png)

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 23, 2019, 6:30am UTC](https://discourse.processing.org/t/restarting-app-programmatically/14658/7 "2019-10-23T06:30:14Z")

</div>

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](https://blog.mindorks.com/android-task-and-back-stack-review-5017f2c18196) 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.
