onPause() and onStop() in Activity(Activity 中的 onPause() 和 onStop())
问题描述
我是 Android 开发新手,但仍然无法理解活动中的 onPause() 和
onStop()
方法.
I am new to Android development and I am still not able to understand the onPause()
and onStop()
methods in an activity.
在我的应用程序中,我有一个名为 Counter 的静态类,它将变量的状态保存在应用程序的内存中.我的应用程序在模拟器中运行良好.我试图测试的是 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
,我希望保留存储在 Counter 类成员中的值,而调用 onStop()
我希望将计数器值重置为零.所以我重写了 onStop()
并将计数器类中的变量设置为零.但是,在模拟器中,我似乎无法让应用程序处于暂停状态.在模拟器中,我打开我的应用程序,练习它.然后我点击模拟器的主页按钮(不是返回按钮),并启动另一个应用程序,相信这会模仿 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
是一种始终em> 保证在您的 Activity
失去焦点时被调用(我说 always 因为在某些设备上,特别是那些运行 Android 3.2+ 的设备上,onStop
并不总是保证在 Activity
被销毁之前被调用).
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).
对于初学者来说,了解 Activity
生命周期的一个好方法是用 Log
乱扔你的重写方法.例如:
A nice way to understand the Activity
lifecycle for beginners is to litter your overriden methods with Log
s. 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 -");
}
}
这篇关于Activity 中的 onPause() 和 onStop()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Activity 中的 onPause() 和 onStop()
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01