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?
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();
}
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?
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.