Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call recreate()?

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!

like image 914
James Jones Avatar asked Apr 18 '15 21:04

James Jones


People also ask

What is recreate () Android?

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() .

What does startActivity do?

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() .


2 Answers

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);
}
like image 152
Tristan Wiley Avatar answered Oct 05 '22 23:10

Tristan Wiley


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.

like image 42
Chamatake-san Avatar answered Oct 06 '22 00:10

Chamatake-san