回答編集履歴

5

setReorderingAllowed(true)

2023/01/20 11:02

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -69,6 +69,7 @@
69
69
  getParentFragmentManager().beginTransaction()
70
70
  .replace(R.id.fragment_container_view, new BFragment())
71
71
  .addToBackStack("AtoB")
72
+ .setReorderingAllowed(true)
72
73
  .commit();
73
74
  });
74
75
  }
@@ -127,6 +128,7 @@
127
128
  getParentFragmentManager().beginTransaction()
128
129
  .replace(R.id.fragment_container_view, new CFragment())
129
130
  .addToBackStack(null)
131
+ .setReorderingAllowed(true)
130
132
  .commit();
131
133
  });
132
134
  }

4

package 消し忘れ

2023/01/20 03:00

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -109,8 +109,6 @@
109
109
  ```
110
110
  BFragment.java
111
111
  ```java
112
- package com.teratail.q_5ffr2ytk1qxm39;
113
-
114
112
  import android.os.Bundle;
115
113
  import android.view.View;
116
114
  import android.widget.Button;

3

追記取り消し

2023/01/20 02:57

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -7,7 +7,6 @@
7
7
 
8
8
  ---
9
9
  質問本文の回答とは少し違うことになりますが、 A→B→C→(back)→A→(back)→終了、です。
10
- (コメントに書いた qiita の記事の解決方法とは少し違います。)
11
10
 
12
11
  MainActivity.java
13
12
  ```java

2

追記

2023/01/20 02:53

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -7,6 +7,7 @@
7
7
 
8
8
  ---
9
9
  質問本文の回答とは少し違うことになりますが、 A→B→C→(back)→A→(back)→終了、です。
10
+ (コメントに書いた qiita の記事の解決方法とは少し違います。)
10
11
 
11
12
  MainActivity.java
12
13
  ```java

1

コード追加

2023/01/20 02:41

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -4,3 +4,213 @@
4
4
  が不要です。
5
5
 
6
6
  フラグメントは基本的には終わらせる必要はありません。
7
+
8
+ ---
9
+ 質問本文の回答とは少し違うことになりますが、 A→B→C→(back)→A→(back)→終了、です。
10
+
11
+ MainActivity.java
12
+ ```java
13
+ import androidx.appcompat.app.AppCompatActivity;
14
+
15
+ import android.os.Bundle;
16
+
17
+ public class MainActivity extends AppCompatActivity {
18
+ @Override
19
+ protected void onCreate(Bundle savedInstanceState) {
20
+ super.onCreate(savedInstanceState);
21
+ setContentView(R.layout.activity_main);
22
+
23
+ if(savedInstanceState == null) {
24
+ getSupportFragmentManager().beginTransaction()
25
+ .replace(R.id.fragment_container_view, new AFragment())
26
+ .commit();
27
+ }
28
+ }
29
+ }
30
+ ```
31
+ res/layout/activity_main.xml
32
+ ```xml
33
+ <?xml version="1.0" encoding="utf-8"?>
34
+ <androidx.constraintlayout.widget.ConstraintLayout
35
+ xmlns:android="http://schemas.android.com/apk/res/android"
36
+ xmlns:app="http://schemas.android.com/apk/res-auto"
37
+ xmlns:tools="http://schemas.android.com/tools"
38
+ android:layout_width="match_parent"
39
+ android:layout_height="match_parent"
40
+ tools:context=".MainActivity">
41
+
42
+ <androidx.fragment.app.FragmentContainerView
43
+ android:id="@+id/fragment_container_view"
44
+ android:layout_width="wrap_content"
45
+ android:layout_height="wrap_content"
46
+ app:layout_constraintBottom_toBottomOf="parent"
47
+ app:layout_constraintEnd_toEndOf="parent"
48
+ app:layout_constraintStart_toStartOf="parent"
49
+ app:layout_constraintTop_toTopOf="parent" />
50
+ </androidx.constraintlayout.widget.ConstraintLayout>
51
+ ```
52
+ AFragment.java
53
+ ```java
54
+ import android.os.Bundle;
55
+ import android.view.View;
56
+ import android.widget.Button;
57
+
58
+ import androidx.annotation.*;
59
+ import androidx.fragment.app.Fragment;
60
+
61
+ public class AFragment extends Fragment {
62
+ public AFragment() {
63
+ super(R.layout.fragment_a);
64
+ }
65
+ @Override
66
+ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
67
+ Button button = view.findViewById(R.id.button);
68
+ button.setOnClickListener(v -> {
69
+ getParentFragmentManager().beginTransaction()
70
+ .replace(R.id.fragment_container_view, new BFragment())
71
+ .addToBackStack("AtoB")
72
+ .commit();
73
+ });
74
+ }
75
+ }
76
+ ```
77
+ res/layout/fragment_a.xml
78
+ ```xml
79
+ <?xml version="1.0" encoding="utf-8"?>
80
+ <androidx.constraintlayout.widget.ConstraintLayout
81
+ xmlns:android="http://schemas.android.com/apk/res/android"
82
+ xmlns:app="http://schemas.android.com/apk/res-auto"
83
+ xmlns:tools="http://schemas.android.com/tools"
84
+ android:layout_width="match_parent"
85
+ android:layout_height="match_parent"
86
+ tools:context=".AFragment">
87
+
88
+ <TextView
89
+ android:id="@+id/textView"
90
+ android:layout_width="wrap_content"
91
+ android:layout_height="wrap_content"
92
+ android:text="AFragment"
93
+ android:textSize="30dp"
94
+ app:layout_constraintBottom_toTopOf="@id/button"
95
+ app:layout_constraintEnd_toEndOf="parent"
96
+ app:layout_constraintStart_toStartOf="parent"
97
+ app:layout_constraintTop_toTopOf="parent" />
98
+ <Button
99
+ android:id="@+id/button"
100
+ android:layout_width="wrap_content"
101
+ android:layout_height="wrap_content"
102
+ android:text="TO B"
103
+ android:textSize="30dp"
104
+ app:layout_constraintBottom_toBottomOf="parent"
105
+ app:layout_constraintEnd_toEndOf="parent"
106
+ app:layout_constraintStart_toStartOf="parent"
107
+ app:layout_constraintTop_toBottomOf="@id/textView" />
108
+ </androidx.constraintlayout.widget.ConstraintLayout>
109
+ ```
110
+ BFragment.java
111
+ ```java
112
+ package com.teratail.q_5ffr2ytk1qxm39;
113
+
114
+ import android.os.Bundle;
115
+ import android.view.View;
116
+ import android.widget.Button;
117
+
118
+ import androidx.annotation.*;
119
+ import androidx.fragment.app.*;
120
+
121
+ public class BFragment extends Fragment {
122
+ public BFragment() {
123
+ super(R.layout.fragment_b);
124
+ }
125
+ @Override
126
+ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
127
+ Button button = view.findViewById(R.id.button);
128
+ button.setOnClickListener(v -> {
129
+ getParentFragmentManager().beginTransaction()
130
+ .replace(R.id.fragment_container_view, new CFragment())
131
+ .addToBackStack(null)
132
+ .commit();
133
+ });
134
+ }
135
+ }
136
+ ```
137
+ res/layout/fragment_b.xml
138
+ ```xml
139
+ <?xml version="1.0" encoding="utf-8"?>
140
+ <androidx.constraintlayout.widget.ConstraintLayout
141
+ xmlns:android="http://schemas.android.com/apk/res/android"
142
+ xmlns:app="http://schemas.android.com/apk/res-auto"
143
+ xmlns:tools="http://schemas.android.com/tools"
144
+ android:layout_width="match_parent"
145
+ android:layout_height="match_parent"
146
+ tools:context=".BFragment">
147
+
148
+ <TextView
149
+ android:id="@+id/textView"
150
+ android:layout_width="wrap_content"
151
+ android:layout_height="wrap_content"
152
+ android:text="BFragment"
153
+ android:textSize="30dp"
154
+ app:layout_constraintBottom_toTopOf="@id/button"
155
+ app:layout_constraintEnd_toEndOf="parent"
156
+ app:layout_constraintStart_toStartOf="parent"
157
+ app:layout_constraintTop_toTopOf="parent" />
158
+ <Button
159
+ android:id="@+id/button"
160
+ android:layout_width="wrap_content"
161
+ android:layout_height="wrap_content"
162
+ android:text="TO C"
163
+ android:textSize="30dp"
164
+ app:layout_constraintBottom_toBottomOf="parent"
165
+ app:layout_constraintEnd_toEndOf="parent"
166
+ app:layout_constraintStart_toStartOf="parent"
167
+ app:layout_constraintTop_toBottomOf="@id/textView" />
168
+ </androidx.constraintlayout.widget.ConstraintLayout>
169
+ ```
170
+ CFragment.java
171
+ ```java
172
+ import android.os.Bundle;
173
+ import android.view.View;
174
+
175
+ import androidx.activity.OnBackPressedCallback;
176
+ import androidx.annotation.*;
177
+ import androidx.fragment.app.*;
178
+
179
+ public class CFragment extends Fragment {
180
+ public CFragment() {
181
+ super(R.layout.fragment_c);
182
+ }
183
+ @Override
184
+ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
185
+ requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) {
186
+ @Override
187
+ public void handleOnBackPressed() {
188
+ getParentFragmentManager().popBackStack("AtoB", FragmentManager.POP_BACK_STACK_INCLUSIVE);
189
+ }
190
+ });
191
+ }
192
+ }
193
+ ```
194
+ res/layout/fragment_c.xml
195
+ ```xml
196
+ <?xml version="1.0" encoding="utf-8"?>
197
+ <androidx.constraintlayout.widget.ConstraintLayout
198
+ xmlns:android="http://schemas.android.com/apk/res/android"
199
+ xmlns:app="http://schemas.android.com/apk/res-auto"
200
+ xmlns:tools="http://schemas.android.com/tools"
201
+ android:layout_width="match_parent"
202
+ android:layout_height="match_parent"
203
+ tools:context=".CFragment">
204
+
205
+ <TextView
206
+ android:id="@+id/textView"
207
+ android:layout_width="wrap_content"
208
+ android:layout_height="wrap_content"
209
+ android:text="CFragment"
210
+ android:textSize="30dp"
211
+ app:layout_constraintBottom_toBottomOf="parent"
212
+ app:layout_constraintEnd_toEndOf="parent"
213
+ app:layout_constraintStart_toStartOf="parent"
214
+ app:layout_constraintTop_toTopOf="parent" />
215
+ </androidx.constraintlayout.widget.ConstraintLayout>
216
+ ```