Certain Fragment in the Back Stack and Show It Again

Android OS provides a back stack office for Activity, information technology also provides the dorsum stack function for Fragment. If yous add together one Fragment into the back stack, when you press the android device back menu, you lot can detect the Fragment that is saved in the dorsum stack popup. Until all the saved Fragments in the back stack popup, then the activity will exit.

android-fragment-back-stack-diagram

one. Fragment Back Stack Instance.

  1. This example contains one activeness and three fragments.
  2. Each fragment includes a button and an input text box.
  3. When clicking the button it will either evidence a subconscious or create a new target fragment.
  4. When clicking the dorsum card, the stacked fragments volition exist popup by society, if the fragment is subconscious in the stack ( fragment two ), then the text in the input box will exist saved besides.
  5. If the fragment is replaced in the stack ( fragment one ), considering the replace action will delete the previous fragment ( fragment ane ) and then add together a new fragment ( fragment ii ), so the input text in the previous fragment will non be saved.
  6. You can apply the below lawmaking to put a fragment into the back stack.
    fragmentTransaction.addToBackStack(null);
  7. If you lot click the "Become To Fragment One" button in Fragment 3, because Fragment i is at the bottom of the back stack, and so it volition remove all the above Fragments in the dorsum stack and prove Fragment One again.
  8. And so when you go to Fragment Two again, you volition find the input text disappear because it is newly created.
  9. Below is this instance demo video ( android fragment back stack example ).

two. Fragment Dorsum Stack Instance Source Lawmaking.

  1. activity_fragment_back_stack.xml
  2. This is the main activity layout XML file.
    <LinearLayout     android:layout_width="match_parent"     android:layout_height="match_parent">      <FrameLayout         android:id="@+id/fragment_back_stack_frame_layout"         android:layout_width="match_parent"         android:layout_height="match_parent" />  </LinearLayout>
  3. fragment_back_stack_one.xml
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="match_parent"     android:layout_height="match_parent">      <TextView         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="This is Fragment One."         android:textSize="20dp"         android:gravity="eye"         android:textColor="@android:colour/holo_red_light"/>      <EditText         android:hint="Input text here."         android:layout_width="match_parent"         android:layout_height="wrap_content" />      <Button         android:id="@+id/fragment_back_stack_one_button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Get To Fragment Two"         android:layout_gravity="heart"         android:textSize="20dp"/>  </LinearLayout>
  4. fragment_back_stack_two.xml
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="match_parent"     android:layout_height="match_parent">      <TextView         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="This is Fragment Two."         android:textSize="20dp"         android:gravity="eye"         android:textColor="@android:colour/holo_green_light"/>      <EditText         android:hint="Input text hither."         android:layout_width="match_parent"         android:layout_height="wrap_content" />      <Push         android:id="@+id/fragment_back_stack_two_button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Become To Fragment Three"         android:layout_gravity="middle"         android:textSize="20dp"/>  </LinearLayout>
  5. fragment_back_stack_three.xml
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="match_parent"     android:layout_height="match_parent">      <TextView         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="This is Fragment Three."         android:textSize="20dp"         android:gravity="center"         android:textColor="@android:color/holo_blue_light"/>      <EditText         android:hint="Input text here."         android:layout_width="match_parent"         android:layout_height="wrap_content" />      <Button         android:id="@+id/fragment_back_stack_three_button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Become To Fragment One"         android:layout_gravity="center"         android:textSize="20dp"/>  </LinearLayout>
  6. FragmentBackStackActivity.coffee: This is the master activeness java file.
    package com.dev2qa.example.fragment.backstack;  import android.os.Parcel; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity;  import com.dev2qa.instance.R; import com.dev2qa.example.util.FragmentUtil;  public course FragmentBackStackActivity extends AppCompatActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_fragment_back_stack);          setTitle("dev2qa.com - Fragment Back Stack Example.");          // Get FragmentManager and FragmentTransaction object.         FragmentManager fragmentManager = getSupportFragmentManager();         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();          // Create FragmentOne instance.         FragmentOne fragmentOne = new FragmentOne();          // Add fragment one with tag name.         fragmentTransaction.add(R.id.fragment_back_stack_frame_layout, fragmentOne, "Fragment Ane");         fragmentTransaction.commit();          FragmentUtil.printActivityFragmentList(fragmentManager);      } }
  7. FragmentOne.java
    package com.dev2qa.example.fragment.backstack;  import android.os.Bundle; import android.support.notation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button;  import com.dev2qa.case.R; import com.dev2qa.instance.util.FragmentUtil;  public course FragmentOne extends Fragment {      @Nullable     @Override     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {         View retView = inflater.inflate(R.layout.fragment_back_stack_one, container, simulated);          final FragmentManager fragmentManager = getFragmentManager();          Button gotoFragmentTwoBtn = (Button)retView.findViewById(R.id.fragment_back_stack_one_button);         gotoFragmentTwoBtn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                  Fragment fragmentTwo = FragmentUtil.getFragmentByTagName(fragmentManager, "Fragment 2");                  // Because fragment ii has been popup from the back stack, so it must be naught.                 if(fragmentTwo==null)                 {                     fragmentTwo = new FragmentTwo();                 }                  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();                 // Replace fragment one with fragment two, the second fragment tag proper noun is "Fragment Ii".                 // This action volition remove Fragment one and add Fragment two.                 fragmentTransaction.replace(R.id.fragment_back_stack_frame_layout, fragmentTwo, "Fragment Two");                  // Add fragment one in back stack.So it will non be destroyed. Press back bill of fare can pop information technology up from the stack.                 fragmentTransaction.addToBackStack(naught);                  fragmentTransaction.commit();                  FragmentUtil.printActivityFragmentList(fragmentManager);             }         });          return retView;     } }
  8. FragmentTwo.coffee
    package com.dev2qa.case.fragment.backstack;  import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Push button;  import com.dev2qa.example.R; import com.dev2qa.instance.util.FragmentUtil;  public class FragmentTwo extends Fragment {      @Nullable     @Override     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Packet savedInstanceState) {         View retView = inflater.inflate(R.layout.fragment_back_stack_two, container, false);          final FragmentManager fragmentManager = getFragmentManager();          Button gotoFragmentThreeBtn = (Button)retView.findViewById(R.id.fragment_back_stack_two_button);         gotoFragmentThreeBtn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 FragmentThree fragmentThree = new FragmentThree();                 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();                  FragmentUtil.printActivityFragmentList(fragmentManager);                  // Get fragment ii if exist.                 Fragment fragmentTwo = FragmentUtil.getFragmentByTagName(fragmentManager, "Fragment 2");                 if(fragmentTwo!=aught) {                     Log.d(FragmentUtil.TAG_NAME_FRAGMENT, "Fragment Two exist in back stack, will hide it now.");                     // Hide fragment two. Only hide not destroy.                     // When user type back carte du jour in Fragment iii,                     // this hidden Fragment will be shown again with input text saved.                     fragmentTransaction.hide(fragmentTwo);                 }                 // Add Fragment with special tag name.                 fragmentTransaction.add(R.id.fragment_back_stack_frame_layout, fragmentThree, "Fragment Three");                 // Add fragment 2 in dorsum stack.                 fragmentTransaction.addToBackStack(nix);                 fragmentTransaction.commit();             }         });          render retView;     } }
  9. FragmentThree.coffee
    package com.dev2qa.instance.fragment.backstack;  import android.os.Bundle; import android.back up.note.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.back up.v4.app.FragmentTransaction; import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button;  import com.dev2qa.instance.R; import com.dev2qa.example.util.FragmentUtil;  public grade FragmentThree extends Fragment {      @Nullable     @Override     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Packet savedInstanceState) {         View retView = inflater.inflate(R.layout.fragment_back_stack_three, container, false);          final FragmentManager fragmentManager = getFragmentManager();          Button gotoFragmentOneBtn = (Button)retView.findViewById(R.id.fragment_back_stack_three_button);         gotoFragmentOneBtn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                  AlertDialog alertDialog = new AlertDialog.Architect(getActivity().getApplicationContext()).create();                  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();                  FragmentUtil.printActivityFragmentList(fragmentManager);                  // Go fragment one if exist.                 Fragment fragmentOne = new FragmentOne();                  fragmentTransaction.supplant(R.id.fragment_back_stack_frame_layout, fragmentOne, "Fragment One");                  // Do not add fragment three in dorsum stack.                 fragmentTransaction.addToBackStack(null);                 fragmentTransaction.commit();             }         });          render retView;     } }
  10. FragmentUtil.java: This is a util java course that provides methods to go existing fragments past the tag proper name, impress debug log, etc.
    package com.dev2qa.case.util;  import android.support.v4.app.Fragment; import android.back up.v4.app.FragmentManager; import android.util.Log;  import java.util.List;  public class FragmentUtil {      public concluding static String TAG_NAME_FRAGMENT = "ACTIVITY_FRAGMENT";      // Get exist Fragment past it's tag proper name.     public static Fragment getFragmentByTagName(FragmentManager fragmentManager, String fragmentTagName)     {         Fragment ret = null;          // Get all Fragment list.         Listing<Fragment> fragmentList = fragmentManager.getFragments();          if(fragmentList!=null)         {             int size = fragmentList.size();             for(int i=0;i<size;i++)             {                 Fragment fragment = fragmentList.get(i);                  if(fragment!=nil) {                     String fragmentTag = fragment.getTag();                      // If Fragment tag name is equal then render it.                     if (fragmentTag.equals(fragmentTagName)) {                         ret = fragment;                     }                 }             }         }          render ret;     }      // Print fragment manager managed fragment in debug log.     public static void printActivityFragmentList(FragmentManager fragmentManager)     {         // Go all Fragment list.         Listing<Fragment> fragmentList = fragmentManager.getFragments();          if(fragmentList!=nada)         {             int size = fragmentList.size();             for(int i=0;i<size;i++)             {                 Fragment fragment = fragmentList.become(i);                  if(fragment!=null) {                     String fragmentTag = fragment.getTag();                     Log.d(TAG_NAME_FRAGMENT, fragmentTag);                 }             }              Log.d(TAG_NAME_FRAGMENT, "***********************************");         }     } }

vancleavedientiong40.blogspot.com

Source: https://www.dev2qa.com/android-fragment-back-stack-example/

0 Response to "Certain Fragment in the Back Stack and Show It Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel