问题
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())?
回答1:
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.
回答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.
来源:https://stackoverflow.com/questions/5530473/how-do-i-restart-an-activity-in-android