Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recreate an activity and also pass arguments

I have an activity that listens to preference changes and reloads the app. I am using recreate() to do that. But I don't know how to pass in arguments through that, so I have resorted to manual activity reloading.

Intent intent = getIntent();
finish();
// add in the arguments as Extras to the intent
startActivity(intent);

This has the behaviour I want, but the recreating of the activity isn't smooth for the user as they will see the activity being killed and the same activity relaunching. I want the user to not be aware that the activity was relaunched. So, my question is can I use the method recreate() and still pass arguments through it.

like image 839
fuadj Avatar asked Aug 04 '16 03:08

fuadj


1 Answers

You can set the data on the activity's intent before calling recreate

        getIntent().putExtra("RECREATE_DATA", "Some Data");
        recreate()

since when you recreate the activity the same activity instance is used, the data in the intent will still be there after recreate.

like image 71
Ismail Shaikh Avatar answered Sep 20 '22 23:09

Ismail Shaikh