在我当前的应用程序中,当我第一次加载 Activity 时,会触发 onResume 函数。我调查了活动生命周期 http://developer.android.com/reference/android/app/Activity.html,但我没有找到一种方法来防止这种情况发生。
我可以在第一次加载 Activity 时阻止 onResume() 函数加载,而不使用 SharedPreferences 吗?
首先,正如 RvdK 所说,您不应该修改 Android Activity 生命周期,您可能必须重新设计您的 Activity 行为才能符合它。
无论如何,这是我看到的最好的方式:
1.在你的Activity中创建一个布尔变量
public class MyActivity extends Activity{
boolean shouldExecuteOnResume;
// The rest of the code from here..
}
2.在 onCreate 中将其设置为 false:
public void onCreate(){
shouldExecuteOnResume = false
}
3.然后在你的onResume中:
public void onResume(){
if(shouldExecuteOnResume){
// Your onResume Code Here
} else{
shouldExecuteOnResume = true;
}
}
这样你的onResume
第一次不会被执行(shouldExecuteOnResume
为 false),但它将在加载活动的所有其他时间执行(因为shouldExecuteOnResume
将是真的)。
如果该活动在下次加载时被终止(由用户或系统)onCreate
方法将被再次调用,所以onResume
不会被执行等等。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)