使用到的Activity
public abstract class BaseActivity extends Activity {
//protected String TAG = getTag();
abstract String getTag();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(getTag(), "onCreate() called with: savedInstanceState = [" + savedInstanceState + "]," + " isFinishing:" + isFinishing());
}
@Override
protected void onStart() {
super.onStart();
Log.d(getTag(), "onStart() called," + " isFinishing:" + isFinishing());
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(getTag(), "onRestart() called," + " isFinishing:" + isFinishing());
}
@Override
protected void onResume() {
super.onResume();
Log.d(getTag(), "onResume() called," + " isFinishing:" + isFinishing());
}
@Override
protected void onPause() {
super.onPause();
Log.d(getTag(), "onPause() called," + " isFinishing:" + isFinishing());
}
@Override
protected void onStop() {
super.onStop();
Log.d(getTag(), "onStop() called," + " isFinishing:" + isFinishing());
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(getTag(), "onDestroy() called," + " isFinishing:" + isFinishing());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(getTag(), "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");
}
}
public class Activity0 extends BaseActivity {
protected String TAG = "Activity0";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_finish);
//finish();
}
@Override
protected void onStart() {
super.onStart();
//finish();
}
@Override
String getTag() {
return TAG;
}
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.button:
//finish();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("video/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://media/external/video/media/63"));
startActivity(intent);
break;
case R.id.button1:
startActivityForResult(new Intent(this, Activity1.class), 200);
break;
}
}
}
public class Activity1 extends BaseActivity {
protected String TAG = "Activity1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_finish2);
}
@Override
String getTag() {
return TAG;
}
public void onClick(View view) {
setResult(RESULT_OK);
}
}
Activity的一些典型生命周期,有些并不会执行中间一些回调.
正常启动,点击按钮finish
Activity0: onRestart() called
Activity0: onStart() called
Activity0: onResume() called, isFinishing:false
===>Button.onClick => finish
Activity0: onPause() called, isFinishing:true
Activity0: onStop() called, isFinishing:true
Activity0: onDestroy() called, isFinishing:true
正常启动,点击BACK
Activity0: onCreate() called with: savedInstanceState = [null]
Activity0: onStart() called
Activity0: onResume() called, isFinishing:false
===>press BACK
Activity0: onPause() called, isFinishing:true
Activity0: onStop() called, isFinishing:true
Activity0: onDestroy() called, isFinishing:true
启动新Activity,setResult并finish
Activity0: onCreate() called with: savedInstanceState = [null]
Activity0: onStart() called
Activity0: onResume() called, isFinishing:false
Activity0: onPause() called, isFinishing:false
===>startActivityForResult
Activity1: onCreate() called with: savedInstanceState = [null]
Activity1: onStart() called
Activity1: onResume() called, isFinishing:false
Activity0: onStop() called, isFinishing:false
===>setResult
Activity1: onPause() called, isFinishing:true
Activity0: onActivityResult() called with: requestCode = [200], resultCode = [-1], data = [null]
Activity0: onRestart() called
Activity0: onStart() called
Activity0: onResume() called, isFinishing:false
Activity1: onStop() called, isFinishing:true
Activity1: onDestroy() called, isFinishing:true
在onCreate里finish
Activity0: onCreate() called with: savedInstanceState = [null], isFinishing:false
Activity0: onDestroy() called, isFinishing:true
在onStart里finish
Activity0: onCreate() called with: savedInstanceState = [null], isFinishing:false
Activity0: onStart() called, isFinishing:false
Activity0: onStop() called, isFinishing:true
Activity0: onDestroy() called, isFinishing:true
启动透明背景的Activity,例如
android:theme="@android:style/Theme.Material.Light.Dialog.NoActionBar"
Activity0: onCreate() called with: savedInstanceState = [null], isFinishing:false
Activity0: onStart() called, isFinishing:false
Activity0: onResume() called, isFinishing:false
===>start transparent Activity
Activity0: onPause() called, isFinishing:false
Activity1: onCreate() called with: savedInstanceState = [null], isFinishing:false
Activity1: onStart() called, isFinishing:false
Activity1: onResume() called, isFinishing:false
===>setResult
Activity1: onPause() called, isFinishing:true
Activity1: onStop() called, isFinishing:true
Activity1: onDestroy() called, isFinishing:true
Activity0: onActivityResult() called with: requestCode = [200], resultCode = [0], data = [null]
Activity0: onResume() called, isFinishing:false
使用ACTION_SEND启动Activity,只会走到onPause
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("video/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://media/external/video/media/63"));
startActivity(intent);
Activity0: onCreate() called with: savedInstanceState = [null], isFinishing:false
Activity0: onStart() called, isFinishing:false
Activity0: onResume() called, isFinishing:false
===>启动Activity,并不会执行onStop
Activity0: onPause() called, isFinishing:false
===>退出Share的Activity
Activity0: onResume() called, isFinishing:false
横竖屏切换
Activity0: onCreate() called with: savedInstanceState = [null], isFinishing:false
Activity0: onStart() called, isFinishing:false
Activity0: onResume() called, isFinishing:false
==>横屏
Activity0: onPause() called, isFinishing:false
Activity0: onStop() called, isFinishing:false
Activity0: onDestroy() called, isFinishing:false
Activity0: onCreate() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@5c9ef82, 2131558525=android.view.AbsSavedState$1@5c9ef82, 2131558526=android.view.AbsSavedState$1@5c9ef82}, android:ActionBar={16909295=android.widget.Toolbar$SavedState@e80c693}, android:Panels={0=com.android.internal.policy.PhoneWindow$PanelFeatureState$SavedState@20786d0, 8=com.android.internal.policy.PhoneWindow$PanelFeatureState$SavedState@d27f6c9}}]}]], isFinishing:false
Activity0: onStart() called, isFinishing:false
Activity0: onResume() called, isFinishing:false