Android:在什么情况下会出现一个Dialog出现导致onPause()被调用? [英] Android: Under what circumstances would a Dialog appearing cause onPause() to be called?

查看:637
本文介绍了Android:在什么情况下会出现一个Dialog出现导致onPause()被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 活动文档的代码段(向下滚动到前景生活行)说:

A snippet from the Android Activities document(scroll down to the "foreground lifetime" line) says :


一个活动可以频繁地进出前景 - 对于
示例,当设备进入睡眠
对话框出现时,调用 onPause()

我不太明白这一点。在什么情况下应该发生这种情况?只有当对话框的上下文与要显示该对话框的活动不同时,才调用 onPause()

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被调用日志条目吗?

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 ()当您的活动不再位于活动堆栈的顶部时调用。对话框本身不是活动,所以不会替换堆栈顶部的当前活动,所以不会导致任何暂停。

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.

一个对话框-case)不需要由Dialog类实现。例如,将主题设置为对话框的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:在什么情况下会出现一个Dialog出现导致onPause()被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆