我有一个应用程序,每次在屏幕上显示时都需要做一些事情。在我的onCreate函数中,应用程序检查用户的设置并执行操作。在某些情况下,用户需要更改其设置才能继续。一旦用户返回应用程序,如何让应用程序再次调用onCreate函数?或者更好的是,当视图出现时调用什么函数?谢谢!
这就是我从我的应用中导航的方式(通过按钮):
if(!networkEnabled){
AlertDialog alertDialog = new AlertDialog.Builder(Main.this).create();
alertDialog.setTitle("Network Location Required");
alertDialog.setMessage("Please enable your network location settings to use the app");
alertDialog.setButton("Open", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent dialogIntent = new Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
}
});
alertDialog.show();
}
然后我正在使用像这样的onStart:
public void onStart(Bundle savedInstanceState) {
super.onStart();
答案 0 :(得分:1)
检查活动的lifecycle。如果Activity从后台返回,则触发的第一个回调是onRestart() - > onStart() - > onResume()。可能你会在onStart()中执行你的代码。调用onStart()后,View将可见。
编辑:
针对这种情况将代码移动到onResume()。一个提示,以更好地了解生命周期。像这样覆盖all callbacks of the lifecycle
并观察Logcat:
public void onStart(Bundle savedInstanceState) {
super.onStart();
Log.w("myActivity","onStart is called");
}
答案 1 :(得分:0)
尝试onRestart()方法
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
}