I
Ikarisan
Neues Mitglied
- 0
Hallo!
Ich habe ein Verständnisproblem mit Fragments. Eigentlich sollte es ganz einfach sein...
Ich habe ein Layout (layout_main.xml) in dem ich innerhalb eines LinearLayout zwei Fragments definiert habe. Das erste zeigt eine immer sichtbare Kopfzeile (FragmentTop) an und das zweite soll, je nach Konfiguration der App, FragmentA bzw. FragmentB anzeigen. Wenn weder A noch B konfiguriert ist (erster Start der App) soll ein FragmentStartupangezeigt werden.
Mein layout_main sieht folgendermaßen aus:
Die dazu gehörenden anderen Layouts sind layout_top
und layout_startup
und layout_a
In meiner MainActivity mache ich jetzt etwas ganz banales:
Leider funktioniert das nicht. Weder das StartupFragment, noch das FragmentA werden angezeigt. Wenn ich jedoch jetzt statt fta.replace(R.id.container, fragment); ein fta.replace(R.id.topLayout, fragment); schreibe wird R.id.container durch FragmentA ersetzt!!
Das verwirrt mich jetzt doch schon ziemlich stark.
Was mache ich denn bloß falsch?
Ich habe ein Verständnisproblem mit Fragments. Eigentlich sollte es ganz einfach sein...
Ich habe ein Layout (layout_main.xml) in dem ich innerhalb eines LinearLayout zwei Fragments definiert habe. Das erste zeigt eine immer sichtbare Kopfzeile (FragmentTop) an und das zweite soll, je nach Konfiguration der App, FragmentA bzw. FragmentB anzeigen. Wenn weder A noch B konfiguriert ist (erster Start der App) soll ein FragmentStartupangezeigt werden.
Mein layout_main sieht folgendermaßen aus:
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:name="com.example.fragment5.FragmentTop"
android:id="@+id/topFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/layout_top" />
<fragment
android:name="com.example.fragment5.FragmentStartup"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/layout_startup" />
</LinearLayout>
Die dazu gehörenden anderen Layouts sind layout_top
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.example.fragment5.FragmentTop"
android:id="@+id/topLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
[...]
und layout_startup
Code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.example.fragment5.FragmentStartup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
[...]
und layout_a
Code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.example.fragment5.FragmentA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
[...]
In meiner MainActivity mache ich jetzt etwas ganz banales:
Code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
// Get a new Fragment Manager
FragmentManager fm = getFragmentManager();
FragmentTransaction fta = fm.beginTransaction();
Fragment fragment = new FragmentA();
// Create a new bundle for the fragment
Bundle args = new Bundle();
// Set the bundle and show the fragment instead of the container
fragment.setArguments(args);
fta.replace(R.id.container, fragment);
fta.commit();
}
Leider funktioniert das nicht. Weder das StartupFragment, noch das FragmentA werden angezeigt. Wenn ich jedoch jetzt statt fta.replace(R.id.container, fragment); ein fta.replace(R.id.topLayout, fragment); schreibe wird R.id.container durch FragmentA ersetzt!!
Das verwirrt mich jetzt doch schon ziemlich stark.

Was mache ich denn bloß falsch?
