当第一次创建活动时,系统调用OnContentChanged()
方法作为第一种方法,系统最后一次调用是OnDetachedFromWindow()
方法,当一个活动被杀死时,但是android文档说整个活动的生命周期发生在OnCreate()
和OnDestroy()
之间。为什么?请帮助我理解这些方法之间的区别。
代码:
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Toast;
public class ActivitylifecycleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onContentChanged() {
super.onContentChanged();
Toast.makeText(getApplicationContext(),"1. onContentChanged()", Toast.LENGTH_SHORT).show();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(getApplicationContext(),"2. onCreate()", Toast.LENGTH_SHORT).show();
}
@Override
public void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(),"3. onStart()", Toast.LENGTH_SHORT).show();
}
@Override
public void onRestoreInstanceState(Bundle restoreInstanceState) {
Toast.makeText(getApplicationContext(),"4. onRestoreinstaneState()", Toast.LENGTH_SHORT).show();
super.onRestoreInstanceState(restoreInstanceState);
}
@Override
public void onRestart() {
super.onRestart();
Toast.makeText(getApplicationContext(),"5. onRestart()", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostCreate(Bundle onpostcrete) {
super.onPostCreate(onpostcrete);
Toast.makeText(getApplicationContext(),"6. onPostCreate()", Toast.LENGTH_SHORT).show();
}
@Override
public void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(),"7. onResume()", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostResume() {
super.onPostResume();
Toast.makeText(getApplicationContext(),"8. onPostResume()", Toast.LENGTH_SHORT).show();
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Toast.makeText(getApplicationContext(),"9. onAttachedToWindow()", Toast.LENGTH_SHORT).show();
}
@Override
public void onWindowFocusChanged(boolean bo) {
super.onWindowFocusChanged(true);
Toast.makeText(getApplicationContext(),"10. onWindowFocusChanged()", Toast.LENGTH_SHORT).show();
}
@Override
public void onUserLeaveHint() {
super.onUserLeaveHint();
Toast.makeText(getApplicationContext(),"11. onUserLeaveHint()", Toast.LENGTH_SHORT).show();
}
@Override
public void onUserInteraction() {
super.onUserInteraction();
ii=0;
Toast.makeText(getApplicationContext(),"12. onUserInteraction()", Toast.LENGTH_SHORT).show();
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Toast.makeText(getApplicationContext(),"13. onSaveInstanceState()", Toast.LENGTH_SHORT).show();
}
@Override
public void onPause() {
super.onPause();
Toast.makeText(getApplicationContext(),"14. onPause()", Toast.LENGTH_SHORT).show();
}
@Override
public void onStop() {
super.onStop();
Toast.makeText(getApplicationContext(),"15. onStop()", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(),"16. onDestroy()", Toast.LENGTH_SHORT).show();
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
Toast.makeText(getApplicationContext(),"17. onDetachedFromWindow()", Toast.LENGTH_SHORT).show();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Toast.makeText(getApplicationContext(),"18. onConfigurationChanged()", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onSearchRequested() {
super.onSearchRequested();
Toast.makeText(getApplicationContext(),"19. onSearchRequested()", Toast.LENGTH_SHORT).show();
return false;
}
}
在此代码中,在onContentChanged()
方法之前调用onCreate()
,在onDetachedFromWindow()
之后调用onDestroy()
。为什么呢?
答案 0 :(得分:19)
<强>的onCreate():强>
当活动开始时,onCreate()
会被调用。它只在活动的生命周期中被调用一次。
<强>的onDestroy():强>
当活动完成其生命周期时,将调用 onDestroy()
。它在活动的生命周期中也被调用一次。
<强> onContentChanged():强>
只要屏幕的内容视图发生更改(由于调用Window.setContentView
或Window.addContentView
),就会调用此挂钩。例如,您可以向活动添加新视图,或者想要通过调用notifyDataSetChanged()
来刷新列表。
<强> onDetachedFromWindow():强>
当与活动关联的主窗口已从窗口管理器中分离时调用。例如,当当前活动进入后台或其他活动进入当前活动时调用它。
答案 1 :(得分:0)
http://developer.android.com/reference/android/app/Activity.html#onContentChanged%28%29
onContentChanged()将不会在用户第一次看到活动性时被调用。
当用户更改移动设备或其他配置的方向时,将调用它。