เมื่อเรียก Fragment ไว้หลายอัน เวลากดปุ่ม Back ย้อนกลับ จะย้อนกลับตามลำดับ Stack
สร้าง Fragment ตามนี้ก่อน การใช้ Fragment Transaction
แล้วแก้ไข 2 ไฟล์นี้
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context=".MainActivity"> <GridLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="10dp"> <Button android:id="@+id/button1" android:text="Fragment 1" android:textSize="16sp" /> <Button android:id="@+id/button2" android:text="Fragment 2" android:textSize="16sp" /> <Button android:id="@+id/button3" android:text="Fragment 3" android:textSize="16sp" /> </GridLayout> <Button android:id="@+id/buttonClose" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="10dp" android:text="Close Current Fragment" android:textAllCaps="false" android:textSize="18sp" /> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package com.phaisarn.myapplication; import android.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.support.v4.app.Fragment; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Fragment1 f1 = new Fragment1(); Button button1 = findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { replaceFragment(f1); } }); final Fragment2 f2 = new Fragment2(); Button button2 = findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { replaceFragment(f2); } }); final Fragment3 f3 = new Fragment3(); Button button3 = findViewById(R.id.button3); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { replaceFragment(f3); } }); final FragmentManager fragMan = getFragmentManager(); Button buttonClose = findViewById(R.id.buttonClose); buttonClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { fragMan.popBackStack(); Toast.makeText(getApplication(), "xx", Toast.LENGTH_SHORT).show(); } }); fragMan.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { @Override public void onBackStackChanged() { if (fragMan.getBackStackEntryCount() == 0) { finish(); //close activity } } }); } private void replaceFragment(Fragment fragment) { getSupportFragmentManager() .beginTransaction() .replace(R.id.frameLayout, fragment) .addToBackStack(null) .commit(); } } |