马会东的博客

马会东的博客

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

简叙 Activity 生命周期及android 返回按钮捕捉

 
复制代码
  @Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState); //To change body of overridden methods use File | Settings | File Templates.
}

@Override
protected void onStart() {
System.out.println("---------------onStart---------------");
super.onStart(); //To change body of overridden methods use File | Settings | File Templates.
}

@Override
protected void onRestart() {
System.out.println("---------------onRestart---------------");
super.onRestart(); //To change body of overridden methods use File | Settings | File Templates.
}

@Override
protected void onResume() {
System.out.println("---------------onResume---------------");
super.onResume(); //To change body of overridden methods use File | Settings | File Templates.
}

@Override
protected void onPostResume() {
System.out.println("---------------onPostResume---------------");
super.onPostResume(); //To change body of overridden methods use File | Settings | File Templates.
}

@Override
protected void onPause() {
System.out.println("---------------onPause---------------");
super.onPause(); //To change body of overridden methods use File | Settings | File Templates.
}

@Override
protected void onStop() {
System.out.println("---------------onStop---------------");
super.onStop(); //To change body of overridden methods use File | Settings | File Templates.
}

@Override
protected void onDestroy() {
System.out.println("---------------onDestroy---------------");
super.onDestroy(); //To change body of overridden methods use File | Settings | File Templates.
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode== KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
Toast.makeText(_context,"点击了返回【onKeyDown】",Toast.LENGTH_LONG).show();
System.out.println("---------------onKeyDown---------------");
this.onBackPressed();
return true; //确认返回了
}
return false; //不允许返回
}

@Override
public void onBackPressed() {

Toast.makeText(_context,"点击了返回【onBackPressed】",Toast.LENGTH_LONG).show();
System.out.println("---------------onBackPressed---------------");
super.onBackPressed();
}
复制代码

简单的说,

   当activity 启动的时候会启动以下方法,按先后顺序

onCreate->onStart->onResume->onPostResume

 

当按返回的时候,也就是结束这个Activity的时候,先后顺序是:

 

onPase->onStop->onDestroy

posted on 2015-05-25 16:18  马会东  阅读(1432)  评论(0编辑  收藏  举报