Android 完成()和活动生命周期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12655898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
finish() and the Activity lifecycle
提问by Karl Giesing
I'm learning Android programming for a class, and I have a quick question about how finish() fits into the Activity lifecycle.
我正在为一个班级学习 Android 编程,我有一个关于 finish() 如何适应 Activity 生命周期的快速问题。
When you make a call to finish(), what lifecycle callback is started? I presume it's onPause(), then onStop() and onDestroy(). Is this correct?
当您调用 finish() 时,将启动什么生命周期回调?我假设它是 onPause(),然后是 onStop() 和 onDestroy()。这样对吗?
Really, I just want to make sure that it doesn't jump straight to onDestroy().
真的,我只是想确保它不会直接跳转到 onDestroy()。
采纳答案by digidigo
回答by ceph3us
Really, I just want to make sure that it doesn't jump straight to onDestroy(). ???
真的,我只是想确保它不会直接跳转到 onDestroy()。???
NO!
不!
but there is one exceptionwhen calling finish() result in activity lifecycle breakthis happens when u call finish() from onCreate() method in which case onDestroy() will be immediately called!
但是当调用 finish() 导致活动生命周期中断时有一个例外,当你从 onCreate() 方法调用 finish() 时会发生这种情况,在这种情况下onDestroy() 将被立即调用!
http://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)
http://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)
回答by Arpit Garg
Yes, it will not jump to onDestroy() skipping the onPause and onStop.
是的,它不会跳到 onDestroy() 跳过 onPause 和 onStop。
Also you might be interested in onPostResume() ,onPostPause() ,onPostCreate(),onUserLeaveHint(), etc .... These are not listed out in the activity life cycle
此外,您可能对onPostResume() 、onPostPause() 、onPostCreate()、onUserLeaveHint()等感兴趣......这些未在活动生命周期中列出
回答by Jan Koester
It could also be very interesting for you to analyze such problems and issues. You can for example set a debuggin-breakpoint in the onPause()method and investigate the program flow from this point.
分析此类问题和问题对您来说也可能非常有趣。例如,您可以在onPause()方法中设置调试断点并从该点调查程序流程。
Also some print-outs can give you some helpful information.
此外,一些打印件可以为您提供一些有用的信息。
You could for example write System.out.println("name of the method" + " called."); in each method of your activity which you think is called. (Overwrite for example onPause(), call super.onPause()and place a console print-out to see if the method is called.
例如,您可以编写 System.out.println("name of the method" + " called."); 在您认为被调用的每个活动方法中。(例如覆盖onPause(),调用super.onPause()并放置控制台打印输出以查看是否调用了该方法。
You will learn a lot about the Android system doing such little investigations while you develop.
在开发过程中,您将通过这些小调查了解很多关于 Android 系统的知识。
回答by mrres1
Create a new Android App and place this in the main activity.
创建一个新的 Android 应用程序并将其放在主要活动中。
Then view the LogCat window (under Android's DDMS) for the outputs
然后查看输出的 LogCat 窗口(在 Android 的 DDMS 下)
Build you application the same − add all the onPause, onStop, etc. methods with outputs to the LogCat.
以相同的方式构建您的应用程序 - 将所有带有输出的 onPause、onStop 等方法添加到 LogCat。
As your program runs you can monitor what is called and at what times.
当您的程序运行时,您可以监控调用的内容和时间。
package com.app.myapp;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyApp extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
Button exit = new Button(this);
exit.setText("finish");
exit.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
Log.v("MyApp", "finish");
finish();
}
});
layout.addView(exit);
setContentView(layout);
Log.v("MyApp", "onCreate");
}
@Override
protected void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
Log.v("MyApp", "onDestroy");
}
@Override
protected void onPause()
{
// TODO Auto-generated method stub
super.onPause();
Log.v("MyApp", "onPause");
}
@Override
protected void onRestart()
{
// TODO Auto-generated method stub
super.onRestart();
Log.v("MyApp", "onRestart");
}
@Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
Log.v("MyApp", "onResume");
}
@Override
protected void onStart()
{
// TODO Auto-generated method stub
super.onStart();
Log.v("MyApp", "onStart");
}
@Override
protected void onStop()
{
// TODO Auto-generated method stub
super.onStop();
Log.v("MyApp", "onStop");
}
}