简体   繁体   English

在OnResume之后立即调用OnPause

[英]OnPause is called right after OnResume

EDIT* This issue is occurring while testing on a android device. 编辑*在Android设备上测试时发生此问题。 This issue does not appear while testing on a emultor. 在对emultor进行测试时,不会出现此问题。


I am starting a new activity and I see onPause is called right after onResume is called. 我正在开始一个新的活动,我看到在onResume被调用后立即调用onPause。 If I look at log it goes to Idle after onResume. 如果我查看日志,它会在onResume之后进入Idle。 Therefore causing onPause right after onResume is called. 因此在调用onResume之后立即导致onPause。

Caller activity - calling MainActivity on onClick via intent. 调用者活动 - 通过意图调用onClick上的MainActivity。

public class TestActivity extends AppCompatActivity implements View.OnClickListener{

    String TAG = "acr";
    Button testBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        testBtn = (Button) findViewById(R.id.testBtn);
        testBtn.setOnClickListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "on pause called on TestActivity ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "on resume called on  TestActivity ");
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.testBtn:
                Intent mainIntent = new Intent(this, MainActivity.class);
                TestActivity.this.startActivity(mainIntent);
                break;
        }
    }
}

Activity that has bug 有bug的活动

public class MainActivity extends AppCompatActivity{

    public static final String TAG = "acrx";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "on pause called on mainactivity");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "on resume Called on Main activity");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "on stop Called on Main activity");
    }
}

Log 日志

12-06 23:24:19.751 22983-22983/com.example.m1alesis.smartcardreader I/acrx: on resume Called on Main activity
12-06 23:24:19.751 22983-22983/com.example.m1alesis.smartcardreader D/SecWifiDisplayUtil: Metadata value : SecSettings2
12-06 23:24:19.751 22983-22983/com.example.m1alesis.smartcardreader D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{5ce8491 I.E...... R.....ID 0,0-0,0}
12-06 23:24:19.781 22983-23012/com.example.m1alesis.smartcardreader D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [1440x2560]-format:1
12-06 23:24:19.811 22983-22983/com.example.m1alesis.smartcardreader W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
12-06 23:24:19.831 22983-22983/com.example.m1alesis.smartcardreader D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
12-06 23:24:19.871 22983-23012/com.example.m1alesis.smartcardreader D/OpenGLRenderer: endAllActiveAnimators on 0x7f9c17ec00 (RippleDrawable) with handle 0x7f9ccc8b60
12-06 23:24:19.871 22983-22983/com.example.m1alesis.smartcardreader I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@fa2f707 time:376798424
12-06 23:24:20.131 22983-22983/com.example.m1alesis.smartcardreader V/ActivityThread: updateVisibility : ActivityRecord{e78cff6 token=android.os.BinderProxy@a67fd36 {com.example.m1alesis.smartcardreader/com.example.m1alesis.smartcardreader.TestActivity}} show : false
12-06 23:24:31.561 22983-22983/com.example.m1alesis.smartcardreader I/acrx: on pause called on mainactivity
12-06 23:24:31.701 22983-22983/com.example.m1alesis.smartcardreader I/acrx: on resume Called on Main activity
12-06 23:24:31.721 22983-22983/com.example.m1alesis.smartcardreader I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@fa2f707 time:376810271

I tried it in my test activity and I did not get your problem. 我在我的测试活动中尝试了它,但我没有得到你的问题。 I had only one button on my MainActivity and my TestActivity. 我的MainActivity和TestActivity上只有一个按钮。

I have found something here: Pausing and Resuming an Activity . 我在这里找到了一些东西: 暂停和恢复活动

This is the interesting part: 这是有趣的部分:

Note: When the system calls your activity's onPause() method, the system may be signaling that the activity will be paused for a moment and the user may return focus to your activity, or that the app is running in multi-window mode. 注意:当系统调用您的活动的onPause()方法时,系统可能会发出活动暂停的信号,并且用户可能会将焦点返回到您的活动,或者该应用程序正在多窗口模式下运行。 However, this method call may also be the first indication that the user is leaving your activity. 但是,此方法调用也可能是用户离开您的活动的第一个指示。

I had the same issue and for me it is related to the activity starting up and the screen not being on - I only saw it after I had the screen off (eg after touching the power button and then home to resume again). 我有同样的问题,对我而言,它与启动的活动和屏幕没有打开有关 - 我只是在屏幕关闭后才看到它(例如,在触摸电源按钮然后回家再次恢复)。

This is a behaviour that others have seen also - I found this question and answer has useful references: OnPause and OnStop() called immediately after starting activity 这是其他人也看到的行为 - 我发现这个问题和答案有用的参考: OnPause和OnStop()在启动活动后立即调用

The key quote that helped me understand the behaviour was: 帮助我理解行为的关键引用是:

Keep in mind that onResume is not the best indicator that your activity is visible to the user; 请记住,onResume不是您的活动对用户可见的最佳指标; a system window such as the keyguard may be in front. 诸如键盘锁之类的系统窗口可以在前面。 Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user 使用onWindowFocusChanged(boolean)可以确定您的活动对用户可见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM