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())?
Asked
Active
Viewed 4.1k times
9
-
Can you not just start another instance then call finish on the original? – Blundell Apr 03 '11 at 15:22
-
@Blundell - the trick is to call `finish()` _before_ starting the new instance. – Ted Hopp Apr 03 '11 at 15:31
-
basically, all I want to do is be able to call the onCreate() method from within the onRestart() method. how would I do that so that the onSaveInstanceState() would save everything. because with this, everything gets reset being that it is a totally new activity? – Ephraim Apr 03 '11 at 16:21
-
@Ephraim - perhaps you could provide more details about what you are trying to accomplish. What you're saying doesn't fit my understanding of the [Activity lifecycle](http://developer.android.com/reference/android/app/Activity.html). Creating, starting, and resuming an activity are completely separate things and you seem to be wanting to mash them together somehow. – Ted Hopp Apr 03 '11 at 19:52
-
@Ted Hopp the way the program works, is, you type in a word, and on the screen, it gives you a curtain number corresponding to that word. if you were to change the settings, the number corresponding to that word would change. the problem is, that the setting won't take effect untill the activity is restarted (if you were to change the orientation, it would allow the setting to take effect, but it won't do it other wise) what I want is to be able to do this as soon as the program starts its onRestart() method. (I want the text in the EditText to stay the same, like in onSaveInstanceState()). – Ephraim Apr 03 '11 at 22:55
-
I'm still not getting what you need. In terms of the activity lifecycle, onRestart() is called only when an application returns to being visible to the user after being hidden. Is that the only condition under which you want the setting change to take effect? Why is overriding onRestart() not enough to get you what you want? (In your original post, you referred to onResume(). The same question applies--why wouldn't just overriding onResume() do what you want?) – Ted Hopp Apr 04 '11 at 14:31
2 Answers
32
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.
-
basically, all I want to do is be able to call the onCreate() method from within the onRestart() method. how would I do that so that the onSaveInstanceState() would save everything. because with this, everything gets reset being that it is a totally new activity? – Ephraim Apr 03 '11 at 16:19
-
1the way the program works, is, you type in a word, and on the screen, it gives you a curtain number corresponding to that word. if you were to change the settings, the number corresponding to that word would change. the problem is, that the setting won't take effect untill the activity is restarted (if you were to change the orientation, it would allow the setting to take effect, but it won't do it other wise) what I want is to be able to do this as soon as the program starts its onRestart() method. (I want the text in the EditText to stay the same, like in onSaveInstanceState()). – Ephraim Apr 04 '11 at 03:12
2
Perhaps you could restart the activity as has been demonstrated, but pass in some intent extras to send your string back when it re-starts.
Intent intent = getIntent();
intent.putExtra(STRINGTOSAVE, "Save this string");
finish();
startActivity(intent);
and in your onCreate you would of course want to retrieve the string
Intent intent = getIntent();
String STRINGTOSAVE = intent.getStringExtra(ActivityName.STRINGTOSAVE);
and then use the retrieved string to reapply the textfield and any other actions you need.

zonabi
- 746
- 6
- 20