来自 Android Activity 文档的片段(向下滚动到前台生存期"行)说:
A snippet from the Android Activities document(scroll down to the "foreground lifetime" line) says :
一个 Activity 可以频繁地进出前台——例如例如,onPause()
在设备进入睡眠状态时调用对话框出现.
An activity can frequently transition in and out of the foreground—for example,
onPause()
is called when the device goes to sleep or when a dialog appears.
我不太明白这个.在什么情况下会发生这种情况?onPause()
是否仅在相关对话框的上下文与要在其上显示对话框的 Activity 不同时调用?
I don't quite understand this. Under what circumstances should this happen? Is onPause()
called only if the context of the dialog in question is different from the activity on top of which the dialog is to be displayed?
根据上述文档中的引用,我的活动的 onPause()
方法是否应该在 AlertDialog
(或只是 Dialog
) 在下面的代码中得到显示?显示对话框时是否应该看到onPause called"日志条目?
Going by the above-mentioned quote from document, should my activity's onPause()
method get called when the AlertDialog
(or just the Dialog
) in the following code gets displayed? Should I see the "onPause called" log entry when the dialog is displayed?
但我不认为会发生这种情况.如果我正确理解了 Android 生命周期,它也不应该!那么,文件指向的是什么?
But I don't see that happen. And it shouldn't either, if I have understood the Android life cycle correctly! So, what's the document pointing at then?
public class LifeCycleTestActivity extends Activity {
private static final String TAG = "LifeCycleTest";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick");
AlertDialog dialog = new AlertDialog.Builder(LifeCycleTestActivity.this).create();
dialog.setMessage("You Clicked on the button");
dialog.setTitle("Dialog!");
dialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.setCancelable(true);
dialog.show();
/*
Dialog dialog = new Dialog(LifeCycleTestActivity.this);
dialog.setTitle("Dialog!");
dialog.setCancelable(true);
dialog.show();
*/
}
});
}
@Override
protected void onPause() {
Log.d(TAG, "onPause() called");
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume() called");
}
}
onPause()
当您的活动不再位于活动堆栈的顶部时调用.Dialog本身不是Activity,所以不会替换栈顶的当前Activity,所以不会造成任何暂停.
onPause()
is called when your activity is no longer at the top of the activity stack. A Dialog by itself is not an Activity, so will not replace the current Activity at the top of the stack, so will not cause anything to pause.
但是,对话框(小写)不需要由 Dialog 类实现.例如,使用主题设置为对话框主题的 Activity 来实现一个并不少见.在这种情况下,显示 dialog-as-an-Activity 将导致新 Activity 位于堆栈顶部,从而暂停之前的位置.
A dialog (lower-case) does not need to be implemented by a Dialog class, however. For example, it is not uncommon to implement one with an Activity whose theme is set to that of a dialog. In this case, displaying the dialog-as-an-Activity will cause the new Activity to be on the top of the stack, pausing what previously was there.
这篇关于Android:在什么情况下出现对话框会导致 onPause() 被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!