来直接搞起他两的生命周期图,有图有真相: 百度百科那边切过来的,感觉两个图在一起,比较有参考意义。
有Activity和Fragment各一个,代码如下:
Activity代码如下:
public class TestFragmentActivity extends FragmentActivity implements TestFragment.OnFragmentInteractionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_fragment);
initView();
Log.e("TestFragmentActivity ","onCreate ");
}
@Override
protected void onStart() {
super.onStart();
Log.e("TestFragmentActivity ","onStart ");
}
@Override
protected void onRestart() {
super.onRestart();
Log.e("TestFragmentActivity ","onRestart ");
}
@Override
protected void onResume() {
super.onResume();
Log.e("TestFragmentActivity ","onResume ");
}
@Override
protected void onPause() {
super.onPause();
Log.e("TestFragmentActivity ","onPause ");
}
@Override
protected void onStop() {
super.onStop();
Log.e("TestFragmentActivity ","onStop ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e("TestFragmentActivity ","onDestroy ");
}
private void initView() {
TestFragment testFragment = TestFragment.newInstance(null, null);
getSupportFragmentManager().beginTransaction()
.replace(R.id.content,testFragment,null).commit();
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
其XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/content"
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"
tools:context="com.example.zhengshizhong.testnewactivity.TestFragmentActivity">
</RelativeLayout>
Fragment:
**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link TestFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link TestFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class TestFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public TestFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.m
* @return A new instance of fragment TestFragment.
*/
// TODO: Rename and change types and number of parameters
public static TestFragment newInstance(String param1, String param2) {
TestFragment fragment = new TestFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.e("TestFragment ","onAttach ");
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("TestFragment ","onCreate ");
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Log.e("TestFragment ","onCreateView ");
return inflater.inflate(R.layout.fragment_test, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("TestFragment ","onActivityCreated ");
}
@Override
public void onStart() {
super.onStart();
Log.e("TestFragment ","onStart ");
}
@Override
public void onResume() {
super.onResume();
Log.e("TestFragment ","onResume ");
}
@Override
public void onPause() {
super.onPause();
Log.e("TestFragment ","onPause ");
}
@Override
public void onStop() {
super.onStop();
Log.e("TestFragment ","onStop ");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.e("TestFragment ","onDestroyView ");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("TestFragment ","onDestroy ");
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
Log.e("TestFragment ","onDetach ");
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
Fragment XML:
<FrameLayout 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"
tools:context="com.example.zhengshizhong.testnewactivity.TestFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="sdfsfdsdfssfdsdfsf"/>
</FrameLayout>
对其输出了日志:
打开界面TESTFragmentActivity:
05-14 16:39:45.985 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragmentActivity: onCreate
05-14 16:39:45.985 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onAttach
05-14 16:39:45.985 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onCreate
05-14 16:39:45.985 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onCreateView
05-14 16:39:45.985 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onActivityCreated
05-14 16:39:45.990 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onStart
05-14 16:39:45.990 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragmentActivity: onStart
05-14 16:39:45.990 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragmentActivity: onResume
05-14 16:39:45.990 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onResume
按back: 05-14 16:41:18.215 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onPause
05-14 16:41:18.215 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragmentActivity: onPause
05-14 16:41:18.585 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onStop
05-14 16:41:18.585 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragmentActivity: onStop
05-14 16:41:18.585 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onDestroyView
05-14 16:41:18.585 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onDestroy
05-14 16:41:18.585 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onDetach
05-14 16:41:18.590 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragmentActivity: onDestroy
打开界面TESTFragmentActivity:
05-14 16:42:02.080 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragmentActivity: onCreate
05-14 16:42:02.080 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onAttach
05-14 16:42:02.080 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onCreate
05-14 16:42:02.080 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onCreateView
05-14 16:42:02.080 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onActivityCreated
05-14 16:42:02.080 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onStart
05-14 16:42:02.080 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragmentActivity: onStart
05-14 16:42:02.080 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragmentActivity: onResume
05-14 16:42:02.080 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onResume
按home键: 05-14 16:42:33.580 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onPause
05-14 16:42:33.580 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragmentActivity: onPause
05-14 16:42:34.115 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onStop
05-14 16:42:34.115 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragmentActivity: onStop
再点开App: 05-14 16:45:03.520 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragmentActivity: onRestart
05-14 16:45:03.520 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onStart
05-14 16:45:03.520 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragmentActivity: onStart
05-14 16:45:03.520 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragmentActivity: onResume
05-14 16:45:03.520 9218-9218/com.example.zhengshizhong.testnewactivity E/TestFragment: onResume
哦了