简叙 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

 

 

 

手机键盘“返回”键判断:

如果在你方法中没有onKeyDown()这个方法,而只有如下构造方法

 public void onBackPressed() {

        Toast.makeText(_context,"点击了返回【onBackPressed】",Toast.LENGTH_LONG).show();
        System.out.println("---------------onBackPressed---------------");
        super.onBackPressed();    //To change body of overridden methods use File | Settings | File Templates.
    }
 

那么,你键盘点击返回的时候,会调用onBackPressed()方法!

 

如果onKeyDown以及onBackPressed两个方法同时存在  会优先运行onKeyDown()方法

 

 public boolean onKeyDown(int keyCode, KeyEvent event) {
        // keyCode 判断用户是否触发键盘“返回按钮”
        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();    //To change body of overridden methods use File | Settings | File Templates.
    }
 

 

 

你可能感兴趣的:(Activity 生命周期,android 返回按钮)