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

五迷三道 提交于 2019-12-03 19:57:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!