ActivityLifeCycle官方demo解析

1.关于Activity的生命周期的几篇文章:

http://1.duoinfo.sinaapp.com/?p=330

http://1.duoinfo.sinaapp.com/?p=332

http://1.duoinfo.sinaapp.com/?p=335

http://1.duoinfo.sinaapp.com/?p=337

training课程给的图是这样的:

ActivityLifeCycle官方demo解析_第1张图片

2.运行官方的程序

ActivityLifeCycle官方demo解析_第2张图片



ActivityLifeCycle官方demo解析_第3张图片



ActivityLifeCycle官方demo解析_第4张图片

ActivityLifeCycle官方demo解析_第5张图片


看到这个demo含有四个Activity,最后一个Activity的样式文件为dialog。并且在下面用TextView显示了每个Activity的状态。

运行程序进入AActivity,此时的状态如下:
ActivityLifeCycle官方demo解析_第6张图片

AActivity进入到onResume方法。分别执行了onCreate--->onStart--->onResume方法。点击Start B按钮,此时的状态如下:
ActivityLifeCycle官方demo解析_第7张图片



看到AActivity进入到onStop方法中,而BActivity则进入到onResume方法中。相应的执行顺序:AActivity接着上面的onResume方法执行了onPause方法,接着BActivity执行了:onCreate--->onStart--->onResume进入到Resumed状态,然后A到了onStop状态,此时点击Start C按钮:我们看到AActivity和BActivity分别进入到Stopped状态,CActivity进入到Resumed状态,同样执行顺序和上面的类似,不再说明,如下图:

ActivityLifeCycle官方demo解析_第8张图片


此时,点击Dialog按钮:

我们看到CActivity进入到Paused状态,说明:在Paused状态下,Activity是可以是半透明的,而不一定完全不可视,点击close关闭Dialog,CActivity进入到Resumed状态,此时点击Finish C:我们看到CActivity执行了onPause--->onDestory进入到Destroyed方法。

ActivityLifeCycle官方demo解析_第9张图片


点击Finish B,和上面的一样的流程,在AActivity的界面上,我们按下小房子,然后再进入:

ActivityLifeCycle官方demo解析_第10张图片

此时我们分析的就差不多了,旋转屏幕同样会带来生命周期的变化。

3.分析源码:

AActivity:

package com.dystu.activitylifecycle;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.dystu.activitylifecycle.utils.StatusTracker;
import com.dystu.activitylifecycle.utils.Utils;


public class AActivity extends ActionBarActivity {

    private String mActivityName;
    private TextView mStatusView;
    private TextView mStatusAllView;
    private StatusTracker mStatusTracker = StatusTracker.getInstance();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        mActivityName = getString(R.string.activity_a);
        mStatusView = (TextView)findViewById(R.id.status_view_a);
        mStatusAllView = (TextView)findViewById(R.id.status_view_all_a);
        mStatusTracker.setStatus(mActivityName, getString(R.string.on_create));
        Utils.printStatus(mStatusView, mStatusAllView);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mStatusTracker.setStatus(mActivityName, getString(R.string.on_start));
        Utils.printStatus(mStatusView, mStatusAllView);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mStatusTracker.setStatus(mActivityName, getString(R.string.on_restart));
        Utils.printStatus(mStatusView, mStatusAllView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mStatusTracker.setStatus(mActivityName, getString(R.string.on_resume));
        Utils.printStatus(mStatusView, mStatusAllView);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mStatusTracker.setStatus(mActivityName, getString(R.string.on_pause));
        Utils.printStatus(mStatusView, mStatusAllView);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mStatusTracker.setStatus(mActivityName, getString(R.string.on_stop));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mStatusTracker.setStatus(mActivityName, getString(R.string.on_destroy));
        mStatusTracker.clear();
    }

    public void startDialog(View v) {
        Intent intent = new Intent(AActivity.this, DialogActivity.class);
        startActivity(intent);
    }

    public void startActivityB(View v) {
        Intent intent = new Intent(AActivity.this, BActivity.class);
        startActivity(intent);
    }

    public void startActivityC(View v) {
        Intent intent = new Intent(AActivity.this, CActivity.class);
        startActivity(intent);
    }

    public void finishActivityA(View v) {
        AActivity.this.finish();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_a, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}







你可能感兴趣的:(android,develop,Android知识点)