14个解决方案
427 votes
你可以简单地使用
finish();
startActivity(getIntent());
从内部刷新Activity。
Sush answered 2019-04-01T02:39:14Z
31 votes
这是我在更改从首选项更改返回后重新加载活动的操作。
@Override
protected void onResume() {
super.onResume();
this.onCreate(null);
}
这实际上导致活动重绘自己。
更新:更好的方法是调用recreate()方法。 这将导致重新创建活动。
kingargyle answered 2019-04-01T02:40:08Z
26 votes
对于那些不想在重新创建()方法之后看到闪烁的人,只需使用
finish();
overridePendingTransition(0, 0);
startActivity(getIntent());
overridePendingTransition(0, 0);
AMAN SINGH answered 2019-04-01T02:40:37Z
10 votes
我需要快速更新我的一个应用程序中的消息列表,所以在关闭我所在的对话框之前,我只是刷新了我的主UI活动。我相信还有更好的方法可以实现这一点。
// Refresh main activity upon close of dialog box
Intent refresh = new Intent(this, clsMainUIActivity.class);
startActivity(refresh);
this.finish(); //
ninehundredt answered 2019-04-01T02:41:04Z
9 votes
Android包括一个进程管理系统,用于处理活动的创建和销毁,这在很大程度上抵消了您从手动重新启动活动中看到的任何好处。 您可以在Application Fundamentals中查看有关它的更多信息
但是,良好的做法是确保onPause和onStop方法释放您不需要保留的任何资源,并使用onLowMemory将您的活动需求减少到绝对最小值。
Al Sutton answered 2019-04-01T02:41:46Z
5 votes
从意图开始activity并关闭activity。
Intent refresh = new Intent(this, Main.class);
startActivity(refresh);//Start the same Activity
finish(); //finish Activity.
Jorgesys answered 2019-04-01T02:42:15Z
4 votes
我看到了之前使用Intent重新加载活动的答案。 这些可以工作但你也可以使用Activity类本身给出的recreate()方法。
而不是写这个
//关闭对话框后刷新主要活动
Intent refresh = new Intent(this, clsMainUIActivity.class);
startActivity(refresh);
this.finish();
这可以通过仅写这个来完成
recreate();
Rizwan answered 2019-04-01T02:43:14Z
2 votes
我有同样的问题
import android.support.v4.content.IntentCompat;
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
这段代码对我有用。Android api 17
Adnan Abdollah Zaki answered 2019-04-01T02:43:50Z
2 votes
在某些情况下,这是其他方面的最佳实践,这不是一个好主意,它是由上下文驱动的如果您选择使用以下内容这是从活动传递给她的儿子的最佳方式:
Intent i = new Intent(myCurrentActivityName.this,activityIWishToRun.class);
startActivityForResult(i, GlobalDataStore.STATIC_INTEGER_VALUE);
事情是每当你完成()从活动希望ToRun你回到你的生活活动
yoav.str answered 2019-04-01T02:44:29Z
1 votes
我认为这不是一个好主意......实施更清洁的方法会更好。 例如,如果您的活动包含表单,则清理方法可以清除表单中的每个窗口小部件并删除所有临时数据。 我想这就是你想要的:将活动恢复到初始状态。
Cristian answered 2019-04-01T02:44:57Z
1 votes
经过一段时间的实验,我发现重新启动活动没有意外的后果。 另外,我认为这与Android在方向改变时默认情况非常类似,因此我没有理由不在类似情况下这样做。
hpique answered 2019-04-01T02:45:27Z
0 votes
我有另一种方法:将我的活动的launchMode设置为singleTop而不拨打finish(),只需startActivity(getIntent())就可以完成这项工作。 只需关注onCreate()和onNewIntent中的新数据。使用Sush的方式,应用程序可能会像AMAN SINGH所说的那样闪烁。 但是AMAN SINGH的做法仍然会创造一种新的活动,即使他修复了“眨眼”问题,我认为这种活动是不必要的。
这个问题太迟了,但如果有人在寻找解决方案,那么就是这样。
WALKER answered 2019-04-01T02:46:12Z
0 votes
简单地用
this.recreate();
这将触发活动中的onCreate方法
Yasiru Nayanajith answered 2019-04-01T02:46:58Z
-1 votes
在活动中,您可以致电recreate()以“重新创建”活动(API 11+)
answered 2019-04-01T02:47:34Z