的onPause()和的onStop()的活动 [英] onPause() and onStop() in Activity

查看:218
本文介绍了的onPause()和的onStop()的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid开发,并有一些东西我还没有理解有关的onPause()的onStop()方法的活动。

I am new to Android development and there is something I am not yet understanding about the onPause() and onStop() methods in an activity.

在我的应用程序,我有我的名字反静态类,它不断变量的状态在内存中的应用程序。我的应用程序正常运行的模拟器。我试图测试是的onPause()的onStop差的行为()

In my app, I have a static class that I name Counter, and it keeps the state of variables in memory for the app. My app runs fine in the emulator. What I was trying to test was differential behavior of onPause() versus onStop().

有关的onPause ,我想保留,而调用的onStop()我存储在Counter类成员的值想要的计数器值复位为零。所以我重写的onStop(),并设置反类中的变量为零。然而,在模拟器中,我似乎无法获得在暂​​停状态下的应用程序。在模拟器中,我打开我的应用程序,行使它。然后我打了模拟器的home键(不后退按钮),并启动其他应用程序,相信这会模仿的onPause()的活动。然而,模拟器似乎并没有兑现这个(我使用的是armeabi V7A仿真器),它似乎总是会被调用的onStop(),因为我的计数器值都回去零,按我在的onStop倍率()。这是固有的模拟器还是我做错了什么让我的活动进入暂停状态?

For onPause, I wanted the values stored in the Counter class's members retained, whereas calling onStop() I wanted the counter values reset to zero. So I override onStop() and set the variables inside the counter class to zero. However, in the emulator, I cannot seem to get the app in the Paused state. In the emulator, I open my app, exercise it. Then I hit the home button (not the back button) of the emulator, and launch another app, believing that this would mimic onPause() activity. However, the emulator does not appear to honor this (I am using an armeabi v7a emulator), it seems to always be calling onStop() because my counter values all go back to zero, per my override in onStop(). Is this inherent to the emulator or am I doing something wrong to get my activity into the paused state?

推荐答案

我不知道该模拟器测试时使用,但的onPause 是一个方法,就是总是的保证被调用时,你的活动失去焦点(我说的总是的,因为在某些设备上,特别是那些运行安卓3.2+,的onStop 并不总是保证活动之前调用被销毁)。

I'm not sure which emulator you are testing with, but onPause is the one method that is always guaranteed to be called when your Activity loses focus (and I say always because on some devices, specifically those running Android 3.2+, onStop is not always guaranteed to be called before the Activity is destroyed).

一个很好的方式,了解活动的生命周期对于初学者是垃圾你重写的方法与登录秒。例如:

A nice way to understand the Activity lifecycle for beginners is to litter your overriden methods with Logs. For example:

public class SampleActivity extends Activity {

    /**
     * A string constant to use in calls to the "log" methods. Its
     * value is often given by the name of the class, as this will 
     * allow you to easily determine where log methods are coming
     * from when you analyze your logcat output.
     */
    private static final String TAG = "SampleActivity";

    /**
     * Toggle this boolean constant's value to turn on/off logging
     * within the class. 
     */
    private static final boolean VERBOSE = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERBOSE) Log.v(TAG, "+++ ON CREATE +++");
    }

    @Override
    public void onStart() {
        super.onStart();
        if (VERBOSE) Log.v(TAG, "++ ON START ++");
    }

   @Override
    public void onResume() {
        super.onResume();
        if (VERBOSE) Log.v(TAG, "+ ON RESUME +");
    }

    @Override
    public void onPause() {
        super.onPause();
        if (VERBOSE) Log.v(TAG, "- ON PAUSE -");
    }

    @Override
    public void onStop() {
        super.onStop();
        if (VERBOSE) Log.v(TAG, "-- ON STOP --");
    }

   @Override
    public void onDestroy() {
        super.onDestroy();
        if (VERBOSE) Log.v(TAG, "- ON DESTROY -");
    }
}

这篇关于的onPause()和的onStop()的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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