Android活动生命周期 - 知道on Resume是OnStart还是Onpause



我想知道是否有任何编程方法来识别活动是否从/随后恢复到onstart()或onpause()?

Android活动生命周期 - 所有这些方法是什么?

预先感谢。

编辑:

我的问题可能还不够清楚。例如,例如,当我的活动从Onpause恢复而不是Onstart时,我想显示烤面包吗?感谢您的时间。

在活动中保持标志。在Onpause中,将其设置为True,然后在Onresume中检查值并相应地执行任务。将标志设置为false在onResume

的末尾。

我想您需要的是 Log class(https://developer.android.com/reference/android/android/util/log.html)。这是其针对Android Studio的教程:https://developer.android.com/studio/debug/am-logcat.html

似乎您出于某种原因要跟踪on-方法,所以只需写:

protected void onStart() {
    Log.d("YourTag", "onStart() is called.");
    super.onStop();
}
protected void onPause() {
    Log.d("YourTag", "onPause() is called.");
    super.onPause();
}

或这是我用于跟踪方法的实用方法:

public static void currentMethod() {
        Throwable t = new Throwable();
        Log.d("SomeTag", "==== " + t.getStackTrace()[1].getClassName() + "#" + t.getStackTrace()[1].getMethodName()
                + " ====");
}

是的,有任何编程方法可以识别活动是从/随后恢复的onstart()还是onpause()。

要理解它,我正在关注一个简单的通知应用程序:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
///it will come in action after code of display notification function described below
DisplayNotification("ActivityLifeCycleDemo","onCreate");
sleepForaMinute();

}
}

现在我们将在活动中添加另外两个支持功能。

static final int NOTIFICATION_ID = 1776;
protected void DisplayNotification(String title,String message)
{
NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.icon, title, System.currentTimeMillis());
PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, ActivityLifeCycleDemo.class), 0);
note.setLatestEventInfo(this, title, message, intent);
notifManager.notify(NOTIFICATION_ID, note);
}
protected void sleepForaMinute()
{
try
{
Thread.sleep(60000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

现在,当您开始活动时,您应该在Android设备上看到下面显示的通知。

ondestroy

现在让我们覆盖ondestroy事件,以便所有的资源都符合:

@Override
protected void onDestroy() {
super.onDestroy();
DisplayNotification("ActivityLifeCycleDemo","onDestroy");
}

OnDestroy方法应清理通过onCreate方法获得的所有资源,并以前被现已破坏的活动使用。

onstart

现在,让我们覆盖onstart方法如下:

@Override
protected void onStart() {
super.onStart();
DisplayNotification("ActivityLifeCycleDemo","onStart");
sleepForaMinute();
}

现在在这里 在Onstart方法中,您应该启动活动所需的所有可见操作,例如显示动画,显示用户提示等。Onstop现在,让我们覆盖脚本的方法如下:

@Override
protected void onStop() {
super.onStop();
DisplayNotification("ActivityLifeCycleDemo","onStop");
}

OnStop方法应停止在Onstart方法中启动的所有动作。由于活动是看不见的,因此您的活动不应执行(并消费CPU周期)Android接口所需的任何任务。on Resume

现在,让我们覆盖on Resume方法如下:

protected void onResume() {
super.onResume();
DisplayNotification("ActivityLifeCycleDemo","onResume");
}

onpause

现在,让我们覆盖Onpause方法如下:

@Override
protected void onPause() {
super.onPause();
DisplayNotification("ActivityLifeCycleDemo","onPause");
}

结论

Android活动在其寿命中经历了几个不同的状态。了解状态和事件将帮助您以更有效,响应式的方式为用户进行编码。只要您仔细地开发不同的事件,Android操作系统通过调用活动中的事件可以有效地管理自己。因此,在管理下一个Android应用的生命周期时玩得开心!

最新更新