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课程给的图是这种:
2.执行官方的程序
分别运行了onCreate--->onStart--->onResume方法。
点击Start Bbutton,此时的状态例如以下:
看到AActivity进入到onStop方法中。而BActivity则进入到onResume方法中。对应的运行顺序:AActivity接着上面的onResume方法运行了onPause方法,接着BActivity运行了:onCreate--->onStart--->onResume进入到Resumed状态,然后A到了onStop状态,此时点击Start Cbutton:我们看到AActivity和BActivity分别进入到Stopped状态,CActivity进入到Resumed状态。相同运行顺序和上面的类似,不再说明,例如以下图:
此时,点击Dialogbutton:
我们看到CActivity进入到Paused状态,说明:在Paused状态下,Activity是能够是半透明的,而不一定全然不可视。点击close关闭Dialog,CActivity进入到Resumed状态,此时点击Finish C:我们看到CActivity运行了onPause--->onDestory进入到Destroyed方法。
点击Finish B。和上面的一样的流程,在AActivity的界面上,我们按下小房子。然后再进入:
此时我们分析的就差点儿相同了,旋转屏幕相同会带来生命周期的变化。
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); } }
版权声明:本文博主原创文章,博客,未经同意不得转载。