2

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?

Zero
  • 1,814
  • 3
  • 20
  • 37

4 Answers4

7

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

Community
  • 1
  • 1
Rachita Nanda
  • 4,389
  • 9
  • 40
  • 64
4

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

Community
  • 1
  • 1
biddulph.r
  • 5,137
  • 3
  • 31
  • 47
1

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.
Sled
  • 17,743
  • 27
  • 115
  • 155
Dulanga
  • 1,316
  • 7
  • 15
0

Include this one ...

startActivity(intent); 
Ganesh Rengarajan
  • 1,998
  • 11
  • 26