Android Fragment Back Stack namunasi
Android Fragment Back Stack namunasi
Android operatsion tizimi Activity uchun back stack funktsiyasini ta’minlaganidek, Fragment uchun ham huddi shu funktsiyani ta’minlaydi. Agar siz biror bir fragmentni back stack ga qo’shib turib, android qurilmangizdagi orqaga chiqish menyusini bossangiz o’sha qo’shilgan fragment paydo bo’ladi. Chunki u back stack da saqlangan. Back Stack dagi barcha fragmentlar ochilganidan so’ng Activity faoliyatini tugatadi.
Fragment Back Stack namunasi
- 1.Bu namuna bitta activity va uchta fragmentni o’z ichiga oladi.
- 2.Har bir fragmentda bittadan button va an input text box bo’ladi.
- 3.Button bosilganda back stackdagi fragment ochiladi yoki yangisi yaratiladi.
- 4.Orqaga chiqish menyusi bosilganda, stackdagi fragmentlar ketma-ketlik bo’yicha paydo bo’laveradi. Agar fragment stack ga qo’shilsa masalan ikkinchi fragment, input box dagi kiritilgan matn ham saqlanadi.
- 5.Agar stackdagi fragment almashtirilsa ya’ni replace harakati amalga oshirilsa, o’zidan oldingi fragmentni o’chirib yuboradi va fragmentning input box idagi matn ham saqlanmaydi.
- 6.Fragmentni back stack ga qo’shishingiz uchun ushbu pasdagi koddan foydalanishingiz mumkin
fragmentTransaction.addToBackStack(null);
- 7.Agar uchinchi fragmentda «Go To Fragment One» (Birinchi fargmentga bor) tugmasini bossangiz, birinchi fragment paydo bo’ladi. Chunki birinchi fragment backstack ning eng pastki qismida, shuning uchun ham o’zidan yuqorida bo’lgan fragmentlarni o’chirib yuboradi
- 8.Va yana ikkinchi fragmentga borsangiz, u yerdagi matn yo’qolganini ko’rasiz. Chunki fragment yangidan yaratilgan
https://www.dev2qa.com/wp-content/uploads/2017/12/android-fragment-back-stack-example.gif
2. Fragment Back Stack namunasining manba kodi
activity_fragment_back_stack.xml
Main activity layout xml file.
fragment_back_stack_one.xml
fragment_back_stack_two.xml
fragment_back_stack_three.xml
FragmentBackStackActivity.java
Main activity java file.
package com.dev2qa.example.fragment.backstack; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import com.dev2qa.example.R; import com.dev2qa.example.util.FragmentUtil; public class 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 One"); fragmentTransaction.commit(); FragmentUtil.printActivityFragmentList(fragmentManager); } }
FragmentOne.java
package com.dev2qa.example.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.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import com.dev2qa.example.R; import com.dev2qa.example.util.FragmentUtil; /** * Created by Jerry on 12/24/2017. */ public class 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, false); 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 Two"); // Because fragment two has been popup from the back stack, so it must be null. if (fragmentTwo == null) { fragmentTwo = new FragmentTwo(); } FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); // Replace fragment one with fragment two, the second fragment tag name is "Fragment Two". // This action will 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 not be destroyed. Press back menu can pop it up from the stack. fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); FragmentUtil.printActivityFragmentList(fragmentManager); } }); return retView; } }
FragmentTwo.java
package com.dev2qa.example.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.Button; import com.dev2qa.example.R; import com.dev2qa.example.util.FragmentUtil; /** * Created by Jerry on 12/24/2017. */ public class FragmentTwo extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle 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 two if exist. Fragment fragmentTwo = FragmentUtil.getFragmentByTagName(fragmentManager, "Fragment Two"); if (fragmentTwo != null) { 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 menu in Fragment three, // 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 two in back stack. fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } }); return retView; } }
FragmentThree.java
package com.dev2qa.example.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.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import com.dev2qa.example.R; import com.dev2qa.example.util.FragmentUtil; /** * Created by Jerry on 12/24/2017. */ public class FragmentThree extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle 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.Builder(getActivity().getApplicationContext()).create(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); FragmentUtil.printActivityFragmentList(fragmentManager); // Get fragment one if exist. Fragment fragmentOne = new FragmentOne(); fragmentTransaction.replace(R.id.fragment_back_stack_frame_layout, fragmentOne, "Fragment One"); // Do not add fragment three in back stack. fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } }); return retView; } }
FragmentUtil.java
Java util class. Bu class mavjud fragmentlarni tag nomi orqali olishimiz va log debug ni yozishimiz uchun metodlar taqdim qiladi.
package com.dev2qa.example.util;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import java.util.List; /** * Created by Jerry on 12/24/2017. */
public class FragmentUtil {
public final static String TAG_NAME_FRAGMENT = "ACTIVITY_FRAGMENT";
// Get exist Fragment by it's tag name.
public static Fragment getFragmentByTagName(FragmentManager fragmentManager, String fragmentTagName) {
Fragment ret = null;
// Get all Fragment list.
List fragmentList = fragmentManager.getFragments();
if (fragmentList != null) {
int size = fragmentList.size();
for (int i = 0; i fragmentList = fragmentManager.getFragments();
if (fragmentList != null) {
int size = fragmentList.size();
for (int i = 0; i Manba:
Mobil Dasturlash
Android Fragment Back Stack namunasi