Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-launch current activity using intent

For an App I am developing, I want to re-launch the current activity using an intent. So I'm in MainActivity.class and I want to re-launch MainActivity.class using the following:

Intent intent = new Intent(getApplicationContext(), MainActivity.class);

This calls onDestroy() but does not re-launch the activity. Why doesn't this work?

like image 470
Zero Avatar asked Feb 05 '26 02:02

Zero


2 Answers

If your are in an Activity: this.recreate();

If your are in a Fragment: getActivity.recreate();

Related Links :

How do I restart an Android Activity

how do I restart an activity in android?

Android activity restart

like image 117
Rachita Nanda Avatar answered Feb 08 '26 19:02

Rachita Nanda


You could just use:

finish();
startActivity(getIntent());

Which will finish the current activity, and start a new activity with the same intent that you received when the activity was originally created. This should effectively re-launch the activity as is.

Edit:

See Reload activity in Android

like image 37
biddulph.r Avatar answered Feb 08 '26 19:02

biddulph.r