Search code examples
androidandroid-activity

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!


Solution

  • 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);
    }