It took me a while to wrap my head around fragments, but this should be my last question about fragments, as I think I just omitted them. I know this is a huge mess of code. But I would appreciate help to make sure I don't break the fundamental rules with fragments.
I am going to publish all my code to see if anyone can โview itโ to see if I am making any serious mistakes or if I should take a simpler route. Finally, as indicated in the title, my snippet is NOT replaced ... it will be added on top.
File tree:

MainActivity.java:
package com.example.learn.fragments; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; public class MainActivity extends FragmentActivity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public static class SSFFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ExampleFragments.java:
package com.example.learn.fragments; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class ExampleFragments extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ActivityMain.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment android:id="@+id/frag" android:layout_width="match_parent" android:layout_height="match_parent" class="com.example.learn.fragments.MainActivity$SSFFragment" /> </RelativeLayout>
choose_pill_frag.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageButton android:id="@+id/imageButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:onClick="blue" android:src="@drawable/blue" /> <ImageButton android:id="@+id/imageButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:onClick="red" android:src="@drawable/red" /> </RelativeLayout>
red_pill_frag.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="You stay in Wonderland and I show you how deep the rabbit-hole goes." android:textAppearance="?android:attr/textAppearanceLarge" /> </RelativeLayout>
The application should display two buttons. Two buttons exist in one fragment, and then, if you press the button, the fragment will be replaced by a new fragment that displays the correct text. At the moment it should replace, but it seems he is adding it on top.
java android android-fragments
EGHDK Jul 23 2018-12-21T00: 00Z
source share