当一个活动第一次被创建时,系统调用OnContentChanged()
方法作为第一个方法,当一个活动被终止时,系统调用的最后一个方法是OnDetachedFromWindow()
方法,但是android docs说一个活动的整个生命周期都发生在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()
之后调用。为什么?
发布于 2012-03-04 20:59:21
http://developer.android.com/reference/android/app/Activity.html#onContentChanged%28%29
当用户第一次看到活动时,将不会调用onContentChanged()。
它将被调用,比如当用户更改mobile..or其他配置的方向时。
https://stackoverflow.com/questions/9554885
复制相似问题