• 快速掌握activity的生命周期


    activity的生命周期不管是在面试还是在工作中我们都会经常遇到,这当然也是非常基础的,基础也很重要哦,学会activity的生命周期对我们以后开发更健壮的程序会有很大帮助。下面来看一下Activity生命周期图:

    为了便于理解,我简单的写了一个Demo,不明白Activity周期的朋友们,可以亲手实践一下,大家按照我的步骤来。

    第一步:新建一个Android工程,我这里命名为MainActivity.再创建一个OtherActivity继承activity。

    源码打印?

    public class MainActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    System.out.println("MainActivity:----------------onCreate--");

    Button button=(Button) this.findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

    // TODO Auto-generated method stub

    Intent intent=new Intent(MainActivity.this,OtherActivity.class);

    startActivity(intent);

    }

    });

    }

    @Override

    protected void onStart() {

    super.onStart();

    // The activity is about to become visible.

    System.out.println("MainActivity:----------------onStart--");

    }

    @Override

    protected void onResume() {

    super.onResume();

    // The activity has become visible (it is now "resumed").

    System.out.println("MainActivity:----------------onResume--");

    }

    @Override

    protected void onPause() {

    super.onPause();

    // Another activity is taking focus (this activity is about to be "paused").

    System.out.println("MainActivity:----------------onPause--");

    }

    @Override

    protected void onStop() {

    super.onStop();

    // The activity is no longer visible (it is now "stopped")

    System.out.println("MainActivity:----------------onStop--");

    }

    @Override

    protected void onDestroy() {

    super.onDestroy();

    // The activity is about to be destroyed.

    System.out.println("MainActivity:----------------onDestroy--");

    }

    }

    源码打印?

    public class OtherActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.other);

    System.out.println("OtherActivity:----------------onCreate--");

    }

    @Override

    protected void onStart() {

    super.onStart();

    // The activity is about to become visible.

    System.out.println("OtherActivity:----------------onStart--");

    }

    @Override

    protected void onResume() {

    super.onResume();

    // The activity has become visible (it is now "resumed").

    System.out.println("OtherActivity:----------------onResume--");

    }

    @Override

    protected void onPause() {

    super.onPause();

    // Another activity is taking focus (this activity is about to be "paused").

    System.out.println("OtherActivity:----------------onPause--");

    }

    @Override

    protected void onStop() {

    super.onStop();

    // The activity is no longer visible (it is now "stopped")

    System.out.println("OtherActivity:----------------onStop--");

    }

    @Override

    protected void onDestroy() {

    super.onDestroy();

    // The activity is about to be destroyed.

    System.out.println("OtherActivity:----------------onDestroy--");

    }

    }

    第三步:运行上述工程,效果图如下(没什么特别的):

    核心在Logcat视窗里,我们打开应用时先后执行了onCreate()->onStart()->onResume三个方法,看一下LogCat视窗如下:

    点击go按钮:

    一定跳转到了 另一个activity界面,下面让我们看看logcat:

    当点击go按钮后首先执行mainActivity的onPause 然后依次执行otherActivity的 onCreate() onStart() onResume()方法,当整个屏幕被另一个activity完全遮挡住了 调用mainActivity的onStop方法.

    接下来点击back按钮:

    这一次先是调用了otherActivity的onPuse方法,失去焦点,然后调用mainActivity的onStart onResume 接着就是otherActivity的停止,销毁。

    从上面可以看出onCreate方法只调用一次,当一个activity失去焦点时,也就是不在最前端时调用onPause方法, 当整个activity不可见时,也就是完全被另一个activity覆盖时,会调用onStop方法。

    下面再让我们看下上面的Activity生命周期图是不是就容易理解了,当失去焦点时调用onPause方法,重新获得焦点调用OnResume方法 这两个方法是相对的。完全被覆盖调用onStop方法,返回前端调用onStart方法。

    然后在点击home键验证一下:

  • 相关阅读:
    JDBCUtils
    C3P0配置
    反射基本常识(二)
    反射基本常识(一)
    String to Integer (atoi)
    ZigZag Conversion
    Longest Palindromic Substring
    Palindrome Number
    Reverse Integer
    Longest Substring Without Repeating Characters
  • 原文地址:https://www.cnblogs.com/xiaochao1234/p/3580788.html
Copyright © 2020-2023  润新知