Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I restart an activity in android? [duplicate]

in an app that I am writing, there is a part of it that allows you to change a curtain setting. the problem is, that this setting won't take effect until the activity is recreated. is there a way to tell the app to restart using the onResume() method (hopefully allowing it to save everything in the onSaveInstanceState())?

like image 235
Ephraim Avatar asked Apr 03 '11 15:04

Ephraim


People also ask

How do I start the same activity again on android?

If you just want to reload the activity, for whatever reason, you can use this. recreate(); where this is the Activity. This is never a good practice. Instead you should startActivity() for the same activity and call finish() in the current one.

How do I know if my android activity is recreated?

You can determine if the activity is finishing by user choice (user chooses to exit by pressing back for example) using isFinishing() in onDestroy . @Override protected void onDestroy() { super. onDestroy(); if (isFinishing()) { // wrap stuff up } else { //It's an orientation change. } }

How do I go back to previous activity?

Navigate to the app > res > layout > activity_main.


1 Answers

This has been posted before:

Intent intent = getIntent();
finish();
startActivity(intent);

As of API level 11, you can also just call an activity's recreate() method. Not only is this cleaner because it is less code, it avoids issues that may arise if your activity was launched by an implicit intent.

like image 97
Ted Hopp Avatar answered Sep 20 '22 07:09

Ted Hopp