Android Fragment boilerplate code
A dummy fragment layout xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".FragmentA"> <TextView style="@style/fragmentTextView" android:text="Fragment A" /> </RelativeLayout>
A dummy Fragment class does nothing but printing the methods names for those methods that’s going to be executed during the lifecyle of a Fragment.
public class FragmentA extends Fragment { private static final String TAG = "FragmentA"; @Override public void onAttach(Context context) { // This is called once the fragment is associated with its activity. super.onAttach(context); Log.i(TAG, " onAttach()"); } @Override public void onCreate(Bundle savedInstanceState) { // This is called after the onAttach(), doing the initial creation of the fragment. super.onCreate(savedInstanceState); Log.i(TAG, " onCreate()"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //This is called after the onCreate(), this is where you will initialize all the views in this fragment. Log.i(TAG, " onCreateView()"); View view = inflater.inflate(R.layout.fragment_a, container, false); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { //This is called after onCreateView(), this is where you can access the resource from the activity of this fragment. super.onActivityCreated(savedInstanceState); Log.i(TAG, " onActivityCreated()"); } @Override public void onStart() { //This is called after onActivityCreated(), it makes the fragment visible to the user (based on its containing activity being started). super.onStart(); Log.i(TAG, " onStart()"); } @Override public void onResume() { //This is called after onStart() super.onResume(); Log.i(TAG, " onResume()"); } @Override public void onPause() { //This is called when the fragment is being moved out from the screen super.onPause(); Log.i(TAG, " onPause()"); } @Override public void onStop() { //This is called when the fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity. super.onStop(); Log.i(TAG, " onStop."); } @Override public void onDestroyView() { //This is where you will clean up resources of this fragment, background thread, handlers, etc. super.onDestroyView(); Log.i(TAG, " onDestroyView."); onDestroy(); } @Override public void onDestroy() { //This is called to do final cleanup of the fragment's state. super.onDestroy(); Log.i(TAG, " onDestroy."); } @Override public void onDetach() { //This is called immediately prior to the fragment no longer being associated with its activity. super.onDetach(); Log.i(TAG, " onDetach()"); } @Override public void onViewStateRestored(Bundle savedInstanceState) { //This is called after onActivityCreated(), this is where you will retrieve the data that's stored from the onSaveInstanceState super.onViewStateRestored(savedInstanceState); String greeting = (savedInstanceState != null) ? savedInstanceState.getString("greeting") : "null"; Log.i(TAG, " onViewStateRestored: " + greeting); } @Override public void onSaveInstanceState(Bundle outState) { //This is called after onPause(), this is where you will store the data that you want to keep when the uer come back to this fragment super.onSaveInstanceState(outState); Log.i(TAG, " onSaveInstanceState."); outState.putString("greeting", "Hello"); } }
To include this fragment in an activity and assign a fragment tag to it.
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initialize and include the FragmentA in this activity FragmentA fragmentA = new FragmentA(); fragTag = "fragment_a"; getFragmentManager() .beginTransaction() .replace(R.id.fragment_place, fragmentA, fragTag) .addToBackStack(null) .commit(); }
The activity_main.xml for the above activity and fragment.
<RelativeLayout 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"> <FrameLayout android:id="@+id/fragment_place" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
To retrieve the fragment by tag in the activity class, the tag name is fragment_a for the fragment created above.
getFragmentManager().findFragmentByTag("fragment_a");
To include fragments in a layout file directly. Assume this is the layout file for an activity, and it is passed to the setContentView in the activity’s onCreate method. In this layout file, there are two fragments, the name attribute of the fragment tag has to be the class path of the fragment. In this case, they are com.example.android.fragments.HeadlinesFragment
and com.example.android.fragments.ArticleFragment
. Reference
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment android:name="com.example.android.fragments.HeadlinesFragment" android:id="@+id/headlines_fragment" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.android.fragments.ArticleFragment" android:id="@+id/article_fragment" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>
Search within Codexpedia
Search the entire web