Android的:在什么情况下会出现一个对话框出现原因的onPause()被调用?对话框、情况下、原因、Android

2023-09-11 20:31:09 作者:愿有岁月可回首

从Android Activities文件(向下滚动到前景一辈子行)说:

  

这是活性可以经常进出前景换的过渡   例如,的onPause()时,该设备进入睡眠状态的的或者当被称为   出现对话框的

我不太明白这一点。在什么情况下应该出现这种情况?为的onPause()只调用如果有问题的对话框的背景下,从顶部的活动,其中的对话框将显示不同?

编辑:添加code样本来说明我的疑问,在细节

从文件走向由上述报价,要我活动的的onPause()方法被调用时, AlertDialog (或只是对话框)以下code被显示出来?我应该看到的onPause称为显示对话框时,日志条目?

但我没有看到这种情况发生。而且它也不应这样,如果我理解正确了Android的生命周期!那么,什么是该文件指向呢?

 公共类LifeCycleTestActivity延伸活动{

    私有静态最后字符串变量=LifeCycleTest;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        按钮BTN =(按钮)findViewById(R.id.button1);

        btn.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                Log.d(TAG的onClick);

                AlertDialog对话框=新AlertDialog.Builder(LifeCycleTestActivity.this).create();
                 dialog.setMessage(你点击了按钮);
                 dialog.setTitle(对话!);
                 dialog.setButton(AlertDialog.BUTTON_NEUTRAL,OK,新DialogInterface.OnClickListener(){

                    @覆盖
                    公共无效的onClick(DialogInterface对话,诠释它){
                        dialog.dismiss();
                    }
                });
                 dialog.setCancelable(真正的);
                 dialog.show();


                / *
                对话对话框=新的对话框(LifeCycleTestActivity.this);
                 dialog.setTitle(对话!);
                 dialog.setCancelable(真正的);
                 dialog.show();
                * /
            }
        });
    }

    @覆盖
    保护无效的onPause(){
        Log.d(TAG的onPause()被称为);
        super.onPause();

    }

    @覆盖
    保护无效onResume(){
        super.onResume();
        Log.d(TAG,onResume()被称为);
    }
}
 

解决方案

在onPause()被调用时,你的活动不再在活动堆栈的顶部。一个对话本身并不是一个活动,所以不会取代当前的活动在堆栈的顶部,所以不会造成任何暂停。

Android Google地图怎么都不显示,求大神给我看看是什么原因,谢谢

一个对话(小写)不需要由一个对话框类来实现,但是。例如,它的情况并不少见,以实现一个与活动的主题为设置为一个对话。在这种情况下,显示该对话框作为安-活动将导致新的活动是在纸叠的顶部,暂停什么previously在那里。

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

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.

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?

EDIT: Adding code sample to illustrate my doubt in detail

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?

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() 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.

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.