本文介绍了Android:场景中调用了onPause而不是onStop?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解onPause和onStop之间的区别.

I'm trying to understand the difference between onPause and onStop.

我已经阅读了所有不同的论坛,但仍不清楚其中的区别.我创建了一个简单的应用程序,以尝试测试何时调用哪种方法.为此,我只是将记录器放在每种方法中.

I've read all the different forums but I still am not clear about the difference. I've created a simple app to try and test when which method gets called. For that I've simply placed loggers in each method.

从我的审判中-

  1. 弹出窗口不会调用任何一种方法
  2. 切换到另一个活动会同时调用这两种方法
  3. 下拉通知栏不会调用任何方法

我只看到两种方法都被快速连续调用,或者根本没有被调用.我正在尝试查找调用onPause但不调用onStop的情况.

I've only seen either both methods being called in quick succession or neither getting called at all.I'm trying to find scenarios where onPause gets called but onStop doesn't.

目的是了解是否甚至需要定义onPause.如果仅调用onPause的情况如此罕见,那么为onPause编写单独的代码甚至没有意义.应该写onStop还不够吗?

The purpose is to understand whether defining onPause is even required. If the scenarios in which only onPause gets called are so rare, it doesn't even make sense to write separate code for onPause. Shouldn't writing onStop be sufficient?

public class LifecycleActivity extends ActionBarActivity {

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("Rachit", "In Destroy Method");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lifecycle);
        Log.d("Rachit", "In Create Method");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("Rachit", "In Start Method");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("Rachit", "In Resume Method");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("Rachit", "In Pause Method");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("Rachit", "In Restart Method");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("Rachit", "In Stop Method");
    }
}

推荐答案

一会儿我想出了这一点,但是忘了在这里发布我的答案.

I figured this out a while later, but forgot to post my answer here.

我发现调用onPause()而没有立即随后调用onStop()的情况是,当我在手机上打开了另一个应用程序时,从另一个应用程序收到了通知.

The instance where I noticed that onPause() was called, without an immediate subsequent call to onStop() was when I got a notification from a different application when a different app was open on my phone.

例如,假设您的手机上当前正在运行Facebook.如果您从WhatsApp收到通知(在弹出对话框中),您的Facebook活动将被暂停.如果您在此期间关闭WhatsApp弹出窗口,Facebook活动将恢复.另一方面,如果您从弹出消息中打开WhatsApp,则您的Facebook活动将停止.

For example, say you have Facebook running on your phone currently. If you receive a notification from WhatsApp (in the pop-up dialog box), your Facebook activity will be paused. If you close the WhatsApp pop-up during this time, the Facebook Activity will get Resumed. On the other hand, if you open WhatsApp from the pop-up message, your Facebook Activity will be stopped.

这篇关于Android:场景中调用了onPause而不是onStop?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 22:41