android - 关于如何让 onResume() 运行 onCreate() 代码的问题
问题描述
我是安卓新手。我有一个关于 onResume() 的问题。从第二个活动返回后,如何使我的活动在 onCreate() 中重新运行代码?(例如,我已经更新了活动 2 中的一些数据,并希望在活动 2 回来后刷新活动 1 以在 onCreate()中运行代码)无论如何更好的做法而不是重新输入onCreate() 到 onResume() 中的代码。抱歉我的英语语法不好,并提前感谢。
解决方案
将您希望运行的部分提取到一个单独的函数中,然后在和onResume()
中调用该函数。onCreate()
onCreate()
onResume()
protected void onCreate(Bundle b){
//Code specific to onCreate call
//...
//Invoke the common function
commonFunction();
}
protected void onResume(){
//Invoke the common function here as well
commonFunction();
}
private void commonFunction(){
//piece of code you want to execute in both onCreate and onResume
//...
}
推荐阅读
- string - 通过硬编码键检索 dict 值,有效。通过计算键检索不会。为什么?
- if-statement - 批发商店 - 如果声明不起作用
- javascript - 在整个页面上注册任何点击href
- swift - 在 SCNPlane (ARKit) 上漫反射动画 SpriteKit 场景
- tmux - 是否可以退出 tmux,同时“保留”终端中的窗口
- vba - 将谷歌脚本结果下载到 Excel VBA
- python - 将字典转换为可以转换的浮点数
- azure-ad-b2c - 无法创建目录
同时正在创建 Azure AD B2C - jenkins - 使用 Jenkins 管道多行/多行字符串参数
- console - OrientDB 3.0.4 控制台 EMBEDDEDLIST 创建顶点