Restart activity using the onResume method

I would like to restart activitiy using the onResume () method. I thought I could use intention to achieve this, but it ends in an endless loop.

@Override protected void onResume() { Intent intent = new Intent(MainActivity.this, MainActivity.class); MainActivity.this.startActivity(intent); finish(); super.onResume(); } 

Is there any other way to restart activity?

+7
source share
2 answers

I would question why you want to do this ... but here is the first thing that appeared in my head:

 @Override protected void onCreate(Bundle savedInstanceState) { ... Log.v("Example", "onCreate"); getIntent().setAction("Already created"); } @Override protected void onResume() { Log.v("Example", "onResume"); String action = getIntent().getAction(); // Prevent endless loop by adding a unique action, don't restart if action is present if(action == null || !action.equals("Already created")) { Log.v("Example", "Force restart"); Intent intent = new Intent(this, Example.class); startActivity(intent); finish(); } // Remove the unique action so the next time onResume is called it will restart else getIntent().setAction(null); super.onResume(); } 

You must make "Already created" unique so that no other intention can accidentally complete this action.

+15
source

Just use this in your onResume ()

@Override protected void onResume() { recreate(); }

0
source

All Articles