こちらのサイトで書かれている下記のコードをfragmentで置き換えようとするとどのような形になるのでしょうか?
Intent intent = new Intent(getApplication(), SubActivity.class); startActivity(intent); finish();
こちらのサイトのように
val nextFragment = NextFragment() val fragmentManager = parentFragmentManager val fragmentTransaction = fragmentManager.beginTransaction() fragmentTransaction.addToBackStack(null) fragmentTransaction.replace(R.id.container,nextFragment) fragmentTransaction.commit() fragmentTransaction.remove(this).commit();
とするとエラーでアプリ自体が強制終了してしまいます。
よろしくお願いいたします。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
ベストアンサー
fragmentTransaction.addToBackStack(null)
と
fragmentTransaction.remove(this).commit();
が不要です。
フラグメントは基本的には終わらせる必要はありません。
質問本文の回答とは少し違うことになりますが、 A→B→C→(back)→A→(back)→終了、です。
MainActivity.java
java
1import androidx.appcompat.app.AppCompatActivity; 2 3import android.os.Bundle; 4 5public class MainActivity extends AppCompatActivity { 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 11 if(savedInstanceState == null) { 12 getSupportFragmentManager().beginTransaction() 13 .replace(R.id.fragment_container_view, new AFragment()) 14 .commit(); 15 } 16 } 17}
res/layout/activity_main.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".MainActivity"> 9 10 <androidx.fragment.app.FragmentContainerView 11 android:id="@+id/fragment_container_view" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 app:layout_constraintBottom_toBottomOf="parent" 15 app:layout_constraintEnd_toEndOf="parent" 16 app:layout_constraintStart_toStartOf="parent" 17 app:layout_constraintTop_toTopOf="parent" /> 18</androidx.constraintlayout.widget.ConstraintLayout>
AFragment.java
java
1import android.os.Bundle; 2import android.view.View; 3import android.widget.Button; 4 5import androidx.annotation.*; 6import androidx.fragment.app.Fragment; 7 8public class AFragment extends Fragment { 9 public AFragment() { 10 super(R.layout.fragment_a); 11 } 12 @Override 13 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 14 Button button = view.findViewById(R.id.button); 15 button.setOnClickListener(v -> { 16 getParentFragmentManager().beginTransaction() 17 .replace(R.id.fragment_container_view, new BFragment()) 18 .addToBackStack("AtoB") 19 .setReorderingAllowed(true) 20 .commit(); 21 }); 22 } 23}
res/layout/fragment_a.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".AFragment"> 9 10 <TextView 11 android:id="@+id/textView" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="AFragment" 15 android:textSize="30dp" 16 app:layout_constraintBottom_toTopOf="@id/button" 17 app:layout_constraintEnd_toEndOf="parent" 18 app:layout_constraintStart_toStartOf="parent" 19 app:layout_constraintTop_toTopOf="parent" /> 20 <Button 21 android:id="@+id/button" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="TO B" 25 android:textSize="30dp" 26 app:layout_constraintBottom_toBottomOf="parent" 27 app:layout_constraintEnd_toEndOf="parent" 28 app:layout_constraintStart_toStartOf="parent" 29 app:layout_constraintTop_toBottomOf="@id/textView" /> 30</androidx.constraintlayout.widget.ConstraintLayout>
BFragment.java
java
1import android.os.Bundle; 2import android.view.View; 3import android.widget.Button; 4 5import androidx.annotation.*; 6import androidx.fragment.app.*; 7 8public class BFragment extends Fragment { 9 public BFragment() { 10 super(R.layout.fragment_b); 11 } 12 @Override 13 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 14 Button button = view.findViewById(R.id.button); 15 button.setOnClickListener(v -> { 16 getParentFragmentManager().beginTransaction() 17 .replace(R.id.fragment_container_view, new CFragment()) 18 .addToBackStack(null) 19 .setReorderingAllowed(true) 20 .commit(); 21 }); 22 } 23}
res/layout/fragment_b.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".BFragment"> 9 10 <TextView 11 android:id="@+id/textView" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="BFragment" 15 android:textSize="30dp" 16 app:layout_constraintBottom_toTopOf="@id/button" 17 app:layout_constraintEnd_toEndOf="parent" 18 app:layout_constraintStart_toStartOf="parent" 19 app:layout_constraintTop_toTopOf="parent" /> 20 <Button 21 android:id="@+id/button" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="TO C" 25 android:textSize="30dp" 26 app:layout_constraintBottom_toBottomOf="parent" 27 app:layout_constraintEnd_toEndOf="parent" 28 app:layout_constraintStart_toStartOf="parent" 29 app:layout_constraintTop_toBottomOf="@id/textView" /> 30</androidx.constraintlayout.widget.ConstraintLayout>
CFragment.java
java
1import android.os.Bundle; 2import android.view.View; 3 4import androidx.activity.OnBackPressedCallback; 5import androidx.annotation.*; 6import androidx.fragment.app.*; 7 8public class CFragment extends Fragment { 9 public CFragment() { 10 super(R.layout.fragment_c); 11 } 12 @Override 13 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 14 requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) { 15 @Override 16 public void handleOnBackPressed() { 17 getParentFragmentManager().popBackStack("AtoB", FragmentManager.POP_BACK_STACK_INCLUSIVE); 18 } 19 }); 20 } 21}
res/layout/fragment_c.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".CFragment"> 9 10 <TextView 11 android:id="@+id/textView" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="CFragment" 15 android:textSize="30dp" 16 app:layout_constraintBottom_toBottomOf="parent" 17 app:layout_constraintEnd_toEndOf="parent" 18 app:layout_constraintStart_toStartOf="parent" 19 app:layout_constraintTop_toTopOf="parent" /> 20</androidx.constraintlayout.widget.ConstraintLayout>
投稿2023/01/19 18:52
編集2023/01/20 11:02総合スコア12648
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/01/19 19:25
2023/01/20 01:36 編集
2023/01/23 11:03