上期回顾了Fragment技术的来龙去脉,今天我主要是复习学习Fragment的生命周期,学习Fragment 的生命周期应该与Activity的生命周期同时复习,顺便做个比较。
Activity的生命周期方法有7个,而Fragment的生命周期方法多达11个。
上面我画了一张图,直角矩形代表的是Fragment的生命周期的过程,圆角矩形飙到的是Activity的生命周期。
下面给出一个实例
package com.fragmentCyle; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MyFragment1 extends Fragment{ @Override public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) { // TODO Auto-generated method stub Log.d("Fragment1", "onInflate"); Log.d("Fragment1_onInflate_activity_stringResource", activity.getResources().getString(R.id.action_settings)); Log.d("fragment1_onInflate_class", attrs.getAttributeValue(null,"class")); //无法获得 Log.d("Fragment1_onInflate_getActivity", String.valueOf(getActivity())); Log.d("Fragment1_onInflate_saveInstancesteate", String.valueOf(savedInstanceState)); super.onInflate(activity, attrs, savedInstanceState); } @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub //无法获得EditText Log.d("Fragment_onAttach_activity_Editext", String.valueOf(activity.findViewById(R.id.myEditText))); Log.d("Fragment_onAttach_activity_Editext", String.valueOf(getActivity().findViewById(R.id.myEditText))); super.onAttach(activity); } @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.first_fragment, null); Log.d("Fragment_onCreateView_activity_Editext", String.valueOf(getActivity().findViewById(R.id.myEditText))); Log.d("Fragment1_onCreateView_EditText", String.valueOf(view.findViewById(R.id.myEditText))); return view; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { // TODO Auto-generated method stub Log.d("Fragment_onViewCreated_activity_Editext", String.valueOf(getActivity().findViewById(R.id.myEditText))); Log.d("Fragment1_onViewCrated_EditText", String.valueOf(view.findViewById(R.id.myEditText))); super.onViewCreated(view, savedInstanceState); } @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub Log.d("Fragment_onActivityCreated_activity_Editext", String.valueOf(getActivity().findViewById(R.id.myEditText))); super.onActivityCreated(savedInstanceState); } @Override public void onStart() { // TODO Auto-generated method stub super.onStart(); } @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); } @Override public void onPause() { // TODO Auto-generated method stub super.onPause(); } @Override public void onStop() { // TODO Auto-generated method stub super.onStop(); } @Override public void onDestroyView() { // TODO Auto-generated method stub super.onDestroyView(); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } @Override public void onDetach() { // TODO Auto-generated method stub super.onDetach(); } }
package com.fragmentCyle; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MyFragment2 extends Fragment{ @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); Log.d("onActivityCreated", "onActivityCreated"); } @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); } @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.second_fragment, null); return view; } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } @Override public void onDestroyView() { // TODO Auto-generated method stub super.onDestroyView(); } @Override public void onDetach() { // TODO Auto-generated method stub super.onDetach(); } @Override public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) { // TODO Auto-generated method stub super.onInflate(activity, attrs, savedInstanceState); } @Override public void onPause() { // TODO Auto-generated method stub super.onPause(); } @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); } @Override public void onStart() { // TODO Auto-generated method stub super.onStart(); } @Override public void onStop() { // TODO Auto-generated method stub super.onStop(); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { // TODO Auto-generated method stub super.onViewCreated(view, savedInstanceState); } }
package com.fragmentCyle; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.Menu; public class FragmentCycleActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
错误分析:
引起这个错误的原因在导入了import android.support.v4.app.Fragment; 导入这个包必须继承FragmentActivity .