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

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

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

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

0回答

880閲覧

fragmentで、remove後にaddしても表示されない

tamago83

総合スコア60

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

0クリップ

投稿2021/03/30 20:50

前提・実現したいこと

1つのactivityで、recyclerviewのlistFragment(一覧画面)から選択して、detailFragment(詳細画面)へ遷移するプログラムを作成しています。

以下のような流れで実施したのですが、remove後のadd(2回目)がうまく動作しません。
・listFragmentで、detailFragmentをaddし、detailFragmentを表示
・removeでdetailFragmentを実行し、listFramentに戻る
・listFragmentで、detailFragmentをaddしても、detailFragmentが表示されない

発生している問題・エラーメッセージ

remove後のadd(2回目)で、detailFragmentが表示されない

該当のソースコード

detailFragment追加箇所

kotlin

1requireActivity().supportFragmentManager.commit { 2 add(R.id.container, fragment, "detailFragment") 3}

detailFragment削除箇所

kotlin

1requireActivity().supportFragmentManager.findFragmentByTag("detailFragment")?.let { detailFragment -> 2 remove(detailFragment) 3}

activity.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<layout xmlns:app="http://schemas.android.com/apk/res-auto" 3 xmlns:android="http://schemas.android.com/apk/res/android"> 4 <androidx.coordinatorlayout.widget.CoordinatorLayout 5 android:layout_width="match_parent" 6 android:layout_height="match_parent"> 7 <com.google.android.material.appbar.AppBarLayout 8 android:id="@+id/app_Bar_layout" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content"> 11 <androidx.appcompat.widget.Toolbar 12 android:id="@+id/toolbar" 13 style="@style/ToolbarTheme" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 app:layout_scrollFlags="scroll|enterAlways"> 17 <TextView 18 android:id="@+id/tooltitle" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:paddingLeft="0dp" 22 android:textColor="@android:color/white" 23 android:maxLines="2" /> 24 </androidx.appcompat.widget.Toolbar> 25 </com.google.android.material.appbar.AppBarLayout> 26 <FrameLayout 27 android:id="@+id/container" 28 android:layout_width="match_parent" 29 android:layout_height="match_parent"> 30 </FrameLayout> 31 </androidx.coordinatorlayout.widget.CoordinatorLayout> 32</layout>

試したこと

detachも試しましたが、うまくいきませんでした。
また、replaceを行えば、問題なく2回目も動くのですが、listFragmentのrecyclerviewの位置が戻ってしまいます。
savedInstanceStateなどで保存すればよいかもしれませんが、listFragmentにviewpagerがあり、その下にさらにfragmentがあるなど、restore対象が多く難しい状況です。

補足情報(FW/ツールのバージョンなど)

android studio: 4.1.3
api level: 30
kotlin_version: 1.4.31

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問