在 Android 中重新加载活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3053761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reload activity in Android
提问by hpique
Is it a good practice to reload an Activity
in Android?
Activity
在Android 中重新加载 an 是一个好习惯吗?
What would be the best way to do it? this.finish
and then this.startActivity
with the activity Intent
?
最好的方法是什么?this.finish
然后this.startActivity
与活动Intent
?
回答by Sush
You can Simply use
你可以简单地使用
finish();
startActivity(getIntent());
to refresh an Activity
from within itself.
Activity
从内部刷新一个。
回答by AMAN SINGH
for those who don't want to see that blink after recreate()method simply use
对于那些不想在 recreate()方法后看到闪烁的人,只需使用
finish();
overridePendingTransition(0, 0);
startActivity(getIntent());
overridePendingTransition(0, 0);
回答by kingargyle
This is what I do to reload the activity after changing returning from a preference change.
这是我在从偏好更改返回后重新加载活动的方法。
@Override
protected void onResume() {
super.onResume();
this.onCreate(null);
}
This essentially causes the activity to redraw itself.
这实质上会导致活动重新绘制自身。
Updated:A better way to do this is to call the recreate()
method. This will cause the activity to be recreated.
更新:执行此操作的更好方法是调用该recreate()
方法。这将导致重新创建活动。
回答by Rizwan
I saw earlier answers which have been given for reloading the activity using Intent. Those will work but you can also do the same using recreate() method given in Activity class itself.
我看到了之前为使用 Intent 重新加载活动而给出的答案。这些会起作用,但您也可以使用 Activity 类本身中给出的 recreate() 方法来做同样的事情。
Instead of writing this
而不是写这个
// Refresh main activity upon close of dialog box
// 关闭对话框时刷新主活动
Intent refresh = new Intent(this, clsMainUIActivity.class);
startActivity(refresh);
this.finish();
This can be done by writing this only
这可以通过只写这个来完成
recreate();
回答by Yasiru Nayanajith
simply use
简单地使用
this.recreate();
this will trigger the onCreate method in the activity
这将触发活动中的 onCreate 方法
回答by ninehundreds
I needed to update a message list in one of my applications in a hurry, so I just performed a refresh of my main UI activity before I closed the dialog I was in. I'm sure there are better ways to accomplish this as well.
我需要匆忙更新我的一个应用程序中的消息列表,所以我只是在关闭我所在的对话框之前刷新了我的主 UI 活动。我相信还有更好的方法来实现这一点。
// Refresh main activity upon close of dialog box
Intent refresh = new Intent(this, clsMainUIActivity.class);
startActivity(refresh);
this.finish(); //
回答by Al Sutton
Android includes a process management system which handles the creation and destruction of activities which largely negates any benefit you'd see from manually restarting an activity. You can see more information about it at Application Fundamentals
Android 包含一个进程管理系统,它处理活动的创建和销毁,这在很大程度上抵消了手动重启活动带来的任何好处。您可以在Application Fundamentals 中查看有关它的更多信息
What is good practice though is to ensure that your onPause and onStop methods release any resources which you don't need to hold on to and use onLowMemoryto reduce your activities needs to the absolute minimum.
好的做法是确保您的 onPause 和 onStop 方法释放您不需要保留的任何资源,并使用onLowMemory将您的活动需求减少到绝对最低限度。
回答by Jorgesys
Start with an intent your same activity
and close the activity
.
以相同的意图开始activity
并关闭activity
.
Intent refresh = new Intent(this, Main.class);
startActivity(refresh);//Start the same Activity
finish(); //finish Activity.
回答by yoav.str
in some cases it's the best practice in other it's not a good idea it's context driven if you chose to do so using the following is the best way to pass from an activity to her sons :
在某些情况下,这是最佳实践,而在其他情况下,如果您选择这样做,则它是上下文驱动的,这不是一个好主意,使用以下是从活动传递给她儿子的最佳方式:
Intent i = new Intent(myCurrentActivityName.this,activityIWishToRun.class);
startActivityForResult(i, GlobalDataStore.STATIC_INTEGER_VALUE);
the thing is whenever you finish() from activityIWishToRun you return to your a living activity
事情是每当你从 activityIWishToRun 完成()时,你就会回到你的生活活动
回答by became_human
Reloading your whole activity may be a heavy task. Just put the part of code that has to be refreshed in (kotlin):
重新加载您的整个活动可能是一项繁重的任务。只需将必须刷新的代码部分放在(kotlin)中:
override fun onResume() {
super.onResume()
//here...
}
Java:
爪哇:
@Override
public void onResume(){
super.onResume();
//here...
}
And call "onResume()" whenever needed.
并在需要时调用“ onResume()”。