Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

Just do this:

  Intent i=getIntent();//This simply returns the intent in which the current Activity is started
  finish();//This would simply stop the current Activity.
  startActivity(i);//This would start a new Activity.

Include this one ...

startActivity(intent); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top