I know this is probably extremely simple, but I just can not figure it out.
I'm trying to reload/recreate an activity after an action. I know I could just use:
Intent intent = getIntent();
finish();
startActivity(intent);
But in reading through answers on the site I'm told to use 'recreate()' after 11 api. Any help would be appreciated, thanks!
Call the recreate() method from where you want to recreate your activity . This method will destroy current instance of Activity with onDestroy() and then recreate activity with onCreate() .
Starting an activity You can start a new instance of an Activity by passing an Intent to startActivity() . The Intent describes the activity to start and carries any necessary data. If you want to receive a result from the activity when it finishes, call startActivityForResult() .
While using the recreate method works by doing
this.recreate()
It was only added in API level 11. If you want to include more devices you can check the API level and implement both the recreate method as well as
Intent intent = getIntent();
finish();
startActivity(intent);
You can use both by making an if statement like...
if (android.os.Build.VERSION.SDK_INT >= 11) {
//Code for recreate
recreate();
} else {
//Code for Intent
Intent intent = getIntent();
finish();
startActivity(intent);
}
this.recreate() is all it takes. Stick that code inside a method that lives in the activity you want to reload. I have a project where this is tied to a button click but you can use it however you need to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With