代码清单1-3:ItemFragment.java package com.enterpriseandroidbook.fragmentframework; import android.app.ActionBar.Tab; import android.app.ActionBar.TabListener; import android.app.Activity; import android.app.Fragment; import android.app.FragmentTransaction; import android.content.res.Configuration; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.FrameLayout; public class ItemFragment extends Fragment implements TabListener, TabbedActivity.SetData { // String for logging the class name private final String CLASSNAME = getClass().getSimpleName(); //Turn logging on or off private final boolean L = true; public void onAttach(Activity activity) { super.onAttach(activity); // Notification that the fragment is associated with an Activity if (L) Log.i(CLASSNAME, "onAttach " + activity.getClass().getSimpleName()); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Notification that Log.i(CLASSNAME, "onCreate"); } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { FrameLayout content = (FrameLayout) inflater.inflate(R.layout.content, container, false); if (L) Log.i(CLASSNAME, "onCreateView"); return content; } public void onStart() { super.onStart(); Log.i(CLASSNAME, "onStart"); } public void onresume() { super.onResume(); Log.i(CLASSNAME, "onResume"); } public void onPause() { super.onPause(); Log.i(CLASSNAME, "onPause"); } public void onStop() { super.onStop(); Log.i(CLASSNAME, "onStop"); } public void onDestroyView() { super.onDestroyView(); Log.i(CLASSNAME, "onDestroyView"); } public void onDestroy() { super.onDestroy(); Log.i(CLASSNAME, "onDestroy"); } public void onDetach() { super.onDetach(); Log.i(CLASSNAME, "onDetach"); } /////////////////////////////////////////////////////////////////////// // Minor lifecycle methods /////////////////////////////////////////////////////////////////////// public void onActivityCreated() { // Notification that the containing activiy and its View hierarchy exist Log.i(CLASSNAME, "onActivityCreated"); } /////////////////////////////////////////////////////////////////////// // Overrides of the implementations ComponentCallbacks methods in Fragment /////////////////////////////////////////////////////////////////////// @Override public void onConfigurationChanged(Configuration newConfiguration) { super.onConfigurationChanged(newConfiguration); // This won't happen unless we declare changes we handle in the manifest if (L) Log.i(CLASSNAME, "onConfigurationChanged"); } @Override public void onLowMemory() { // No guarantee this is called before or after other callbacks if (L) Log.i(CLASSNAME, "onLowMemory"); } /////////////////////////////////////////////////////////////////////// // Implementation of TabListener ///////////////////////////////////////////////////////////////////////
同所有需要响应生命周期方法的其他类一样,这里实现了这些方法并记录了日志。所以你可以很容易地看到何时调用它们。
以下三个方法实现TabListener接口。把它们传入一个FragmentTransaction对象,该对象聚合了在片段之间导航时所发生的所有操作。这里,只需显示或隐藏片段。
@Override public void onTabReselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { ft.show(this); } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { ft.hide(this); } /////////////////////////////////////////////////////////////////////// // Implementation of SetData /////////////////////////////////////////////////////////////////////// 下面是setData()方法,它实现SetData接口。此接口告诉片段哪些数据应该显示。 @Override public void setData(Bundle data) { // Display the number EditText t = (EditText) getActivity().findViewById(R.id.editText1); int i = data.getInt("place"); t.setText(Integer.toString(i)); } }