質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

536閲覧

Androidのfragmentについて

sena14

総合スコア109

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2023/01/19 17:39

こちらのサイトで書かれている下記のコードを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ページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答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
jimbe

総合スコア12648

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sena14

2023/01/19 19:25

消してみたのですがおかしな挙動になってしまいます。 FragmentA,B,Cがあり A→B→Cという形で画面遷移します。 A→Bのタイミングで fragmentTransaction.addToBackStack(null)を消した場合 Bでバックキーが一度目反応せずに二度目でアプリが閉じる B→Cのタイミングで試すと Cでバックキーを押すとAに戻ることから実装出来ていると思いきや、Aでバックキーを押すと再度Cに遷移してしまいます。 ActivityでいうとB→Cのタイミングでintentで画面遷移した後にBをfinishさせつつCに移動するというのが希望の形です。そしてCでバックキーを押すとAに戻る。Aでバックキーを押すとアプリが閉じるというのが理想です。 上記の様な仕様の場合は何が間違っているのでしょうか?
jimbe

2023/01/20 01:36 編集

先の質問でも書きましたが、 addToBackStack の動作を調べてください。恐らく画面の状態を保存すると勘違いされていますが、そうではありません。(ネットの記事でもそのように書かれてしまっているものが結構あります。) Fragmentによる画面遷移でハマった https://qiita.com/tomo1139/items/62902093d1850085742d
sena14

2023/01/23 11:03

ありがとうございます。 解決できました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問