1.activity生命周期
(1)打开A,a-oncreate,a-onstart,a-onresume
(2)A上按钮打开B,a-onpause,b-oncreate,b-onstart,b-onresume,a-onStop
(3)B返回A,b-onPause,a-onstart,a-onresume,b-onStop,b-onDestory
2.fragment生命周期
(1)cActivity中装fragmentA
cActivity-onCreate,
fragmentA-onAttach,
fragmentA-onCreate,
fragmentA-onCreateView,
fragmentA-onCreateAnimation,
fragmentA-onViewCreated,
fragmentA-onActivityCreated,
fragmentA-onViewStateRestored,
fragmentA-onStart,
cActivity-onStart,
cActivity-onResume,
fragmentA-onResume
(2)按返回
fragmentA-onPause,
cActivity-onPause,
fragmentA-onStop,
cActivity-onStop,
fragmentA-onDestroyView,
fragmentA-onDestroy,
fragmentA-onDetach,
Activity-onDestroy
(3)若在fragmentA打开fragmentB
AFragment--onPause
AFragment--onStop
AFragment--onDestroyView
AFragment--onCreateAnimation
BFragment--onAttach
BFragment--onCreate
BFragment--onCreateView
BFragment--onCreateAnimation
BFragment--onViewCreated
BFragment--onActivityCreated
BFragment--onViewStateRestored
BFragment--onStart
BFragment--onResume
(4)按返回
BFragment--onPause
BFragment--onStop
BFragment--onDestroyView
BFragment--onCreateAnimation
BFragment--onDestroy
BFragment--onDetach
AFragment--onCreateView
AFragment--onCreateAnimation
AFragment--onViewCreated
AFragment--onActivityCreated
AFragment--onViewStateRestored
AFragment--onStart
AFragment--onResume
(5)若在fragmentA中打开activityA
AFragment--onPause
CActivity--onPause
AActivity--onCreate
AActivity--onStart
AActivity--onResume
AFragment--onSaveInstanceState
AFragment--onStop
CActivity--onStop
(6)按返回
AActivity--onPause
AFragment--onStart
CActivity--onStart
CActivity--onResume
AFragment--onResume
AActivity--onStop
AActivity--onDestroy
(7)若在fragmentA中打开activityA,并finsh掉activityC
AFragment-- onPause
CActivity-- onPause
AActivity-- onCreate
AActivity-- onStart
AActivity-- onResume
AFragment-- onStop
CActivity-- onStop
AFragment-- onDestroyView
AFragment-- onDestroy
AFragment-- onDetach
CActivity-- onDestroy
具体的代码,如下
/**
* @author jiagf @email [email protected]
* @date 2015-2-4
* @version 0.2
* @description
*/
package com.jia.demo.activity.life;
import com.jia.demo.R;
import com.jia.demo.activity.BaseActivity;
import com.jia.demo.util.LogUtil;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
/**
* @author Mars
*
*/
public class AActivity extends BaseActivity {
private String TAG = this.getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
this.findViewById(R.id.a_btn).setOnClickListener(click_listener);
LogUtil.e(TAG, "onCreate");
}
OnClickListener click_listener = new OnClickListener() {
@Override
public void onClick(View v) {
startActvity(getApplicationContext(), BActivity.class);
}
};
@Override
protected void onStart() {
super.onStart();
LogUtil.e(TAG, "onStart");
}
@Override
protected void onResume() {
super.onResume();
LogUtil.e(TAG, "onResume");
}
@Override
protected void onPause() {
super.onPause();
LogUtil.e(TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
LogUtil.e(TAG, "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
LogUtil.e(TAG, "onDestroy");
}
}
/**
* @author jiagf @email [email protected]
* @date 2015-2-4
* @version 0.2
* @description
*/
package com.jia.demo.activity.life;
import com.jia.demo.R;
import com.jia.demo.activity.BaseActivity;
import com.jia.demo.util.LogUtil;
import android.os.Bundle;
/**
* @author Mars
*
*/
public class BActivity extends BaseActivity {
private String TAG = this.getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
LogUtil.e(TAG, "onCreate");
}
@Override
protected void onStart() {
super.onStart();
LogUtil.e(TAG, "onStart");
}
@Override
protected void onResume() {
super.onResume();
LogUtil.e(TAG, "onResume");
}
@Override
protected void onPause() {
super.onPause();
LogUtil.e(TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
LogUtil.e(TAG, "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
LogUtil.e(TAG, "onDestroy");
}
}
/**
* @author jiagf @email [email protected]
* @date 2015-2-4
* @version 0.2
* @description
*/
package com.jia.demo.activity.life;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import com.jia.demo.R;
import com.jia.demo.fragment.life.AFragment;
import com.jia.demo.util.LogUtil;
/**
* @author Mars
*
*/
public class CActivity extends FragmentActivity {
private String TAG = this.getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
AFragment fragment = new AFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.nav_content_fragment,fragment);
fragmentTransaction.commit();
LogUtil.e(TAG, "onCreate");
}
OnClickListener click_listener = new OnClickListener() {
@Override
public void onClick(View v) {
}
};
@Override
protected void onStart() {
super.onStart();
LogUtil.e(TAG, "onStart");
}
@Override
protected void onResume() {
super.onResume();
LogUtil.e(TAG, "onResume");
}
@Override
protected void onPause() {
super.onPause();
LogUtil.e(TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
LogUtil.e(TAG, "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
LogUtil.e(TAG, "onDestroy");
}
}
/**
* @author jiagf @email [email protected]
* @date 2015-2-4
* @version 0.2
* @description
*/
package com.jia.demo.fragment.life;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import com.jia.demo.R;
import com.jia.demo.activity.life.AActivity;
import com.jia.demo.fragment.BaseFragment;
import com.jia.demo.util.LogUtil;
public class AFragment extends BaseFragment {
private String TAG = this.getClass().getSimpleName();
private View root;
// 标志位,标志已经初始化完成。
private boolean isPrepared;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onDestroy() {
super.onDestroy();
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onDetach() {
super.onDetach();
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
root = inflater.inflate(R.layout.fragment_a, container, false);
isPrepared = true;
lazyLoad();
return root;
}
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onDestroyView() {
super.onDestroyView();
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onPause() {
super.onPause();
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onResume() {
super.onResume();
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onStart() {
super.onStart();
setTitleName("A");
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onStop() {
super.onStop();
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onLowMemory() {
super.onLowMemory();
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onInflate(Activity activity, AttributeSet attrs,
Bundle savedInstanceState) {
super.onInflate(activity, attrs, savedInstanceState);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public boolean onContextItemSelected(MenuItem item) {
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
return super.onContextItemSelected(item);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
return super.onCreateAnimation(transit, enter, nextAnim);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onDestroyOptionsMenu() {
super.onDestroyOptionsMenu();
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
return super.onOptionsItemSelected(item);
}
@Override
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed(menu);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
protected void lazyLoad() {
LogUtil.e(TAG,"isPrepared:"+isPrepared+",isVisible:"+isVisible);
// if(!isPrepared || !isVisible) {
// return;
// }
//填充各控件的数据
root.findViewById(R.id.fragment_a_btn).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// startFragment(R.id.nav_content_fragment, new BFragment(), "bFragment");
startActvity(getActivity(), AActivity.class);
getActivity().finish();
}
});
}
}
/**
* @author jiagf @email [email protected]
* @date 2015-2-4
* @version 0.2
* @description
*/
package com.jia.demo.fragment.life;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Button;
import com.jia.demo.R;
import com.jia.demo.fragment.BaseFragment;
import com.jia.demo.util.LogUtil;
public class BFragment extends BaseFragment {
private String TAG = this.getClass().getSimpleName();
private View root;
// 标志位,标志已经初始化完成。
private boolean isPrepared;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onDestroy() {
super.onDestroy();
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onDetach() {
super.onDetach();
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
root = inflater.inflate(R.layout.fragment_a, container, false);
isPrepared = true;
lazyLoad();
return root;
}
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onDestroyView() {
super.onDestroyView();
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onPause() {
super.onPause();
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onResume() {
super.onResume();
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onStart() {
super.onStart();
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onStop() {
super.onStop();
setTitleName("B");
showReturnButton(true);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onLowMemory() {
super.onLowMemory();
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onInflate(Activity activity, AttributeSet attrs,
Bundle savedInstanceState) {
super.onInflate(activity, attrs, savedInstanceState);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public boolean onContextItemSelected(MenuItem item) {
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
return super.onContextItemSelected(item);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
return super.onCreateAnimation(transit, enter, nextAnim);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onDestroyOptionsMenu() {
super.onDestroyOptionsMenu();
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
return super.onOptionsItemSelected(item);
}
@Override
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed(menu);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
LogUtil.e(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
}
@Override
protected void lazyLoad() {
LogUtil.e(TAG,"isPrepared:"+isPrepared+",isVisible:"+isVisible);
// if(!isPrepared || !isVisible) {
// return;
// }
setTitleName("B");
//填充各控件的数据
Button a =(Button)root.findViewById(R.id.fragment_a_btn);
a.setText(getActivity().getResources().getString(R.string.ipsum));
}
}
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:clipToPadding="false" >
<Button
android:id="@+id/a_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="18sp"
android:text="@string/click" />
</ScrollView>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:clipToPadding="false" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="18sp"
android:text="@string/ipsum" />
</ScrollView>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="80dp"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/nav_content_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:clipToPadding="false" >
<Button
android:id="@+id/fragment_a_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="18sp"
android:text="点击" />
</ScrollView>