M
markusk73
Neues Mitglied
- 2
Hallo,
ich beschäftige mich grad mit den Fragmenten und zu diesem Zweck hab ich mir ein ganz simples Fragment-Layout gestrickt, dieses dreimal direkt ins Layout der Main-Activity eingefügt und noch drei Buttons mit dem ich das jeweilige Fragment über eine Transaktion mit remove entfernen wollte aber das Fragment wird immer noch angezeigt.
Hier das Layout der Main Activity:
Und hier der Code im OnClickListener zum Entfernen des zweiten Fragments:
Wieso bleibt das Fragment nach dem remove noch immer sichtbar?
lg, Markus
ich beschäftige mich grad mit den Fragmenten und zu diesem Zweck hab ich mir ein ganz simples Fragment-Layout gestrickt, dieses dreimal direkt ins Layout der Main-Activity eingefügt und noch drei Buttons mit dem ich das jeweilige Fragment über eine Transaktion mit remove entfernen wollte aber das Fragment wird immer noch angezeigt.
Hier das Layout der Main Activity:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment"
android:name="com.myapps.myfragments.Fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/fragment2"
android:name="com.myapps.myfragments.Fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/fragment3"
android:name="com.myapps.myfragments.Fragment3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp">
<Button
android:id="@+id/btnMainFragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/fragment1_main_btn_label"
app:backgroundTint="@android:color/holo_blue_dark" />
<Button
android:id="@+id/btnMainFragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/fragment2_main_btn_label"
app:backgroundTint="@android:color/holo_blue_dark" />
<Button
android:id="@+id/btnMainFragment3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/fragment3_main_btn_label"
app:backgroundTint="@android:color/holo_blue_dark" />
<TextView
android:id="@+id/tvMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
</LinearLayout>
</LinearLayout>
Code:
FragmentManager fm = getSupportFragmentManager();
Fragment2 fragment = (Fragment2) fm.findFragmentById(R.id.fragment2);
if (fragment != null) {
Log.d("xyz", String.valueOf(fragment.getId()));
FragmentTransaction ft = fm.beginTransaction();
ft.setReorderingAllowed(true);
ft.remove((Fragment2) fragment);
ft.commit();
}
Wieso bleibt das Fragment nach dem remove noch immer sichtbar?
lg, Markus