Fragment replacement does not work / Am I doing it right?

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:

enter image description here

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; } /* Add a class to handle fragment */ public static class SSFFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v = inflater.inflate(R.layout.choose_pill_frag, container, false); return v; } } public void red(View view) { // Create new fragment and transaction ExampleFragments newFragment = new ExampleFragments(); android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack transaction.replace(R.id.frag, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit(); } public void blue(View view) { //Figure out code for "red" first } } 

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) { // Inflate the layout for this fragment return inflater.inflate(R.layout.red_pill_frag, container, false); } } 

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.

+18
java android android-fragments
Jul 23 2018-12-21T00:
source share
5 answers

Instead of <fragment> use <FrameLayout> in the xml layout for activity.

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container_id" android:layout_width="match_parent" android:layout_height="match_parent" /> 

Then in the FragmentActivity in onCreate add the source fragment (in your case SSFFragment ):

  FragmentA fragmentA = new FragmentA(); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.add(R.id.container_id, fragmentA); transaction.commit(); 

From the inner fragment, you can replace the fragment in the container.

 class FragmentA extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Button button = new Button(getActivity()); button.setText("Replace"); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); FragmentB fragmentB = new FragmentB(); transaction.replace(R.id.container_id, fragmentB); transaction.commit(); } }); return button; } } 
+24
Jul 29 2018-12-12T00:
source share

Here is the answer to your real question ... since this was your second question that arose from your original post , I changed the solution to get frag from this differently:

 Fragment details = (Fragment)getSupportFragmentManager().findFragmentById(R.id.details); details = new ExamplesFragment(); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.details, details); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); ft.commit(); 

In addition, part of android.support.v4.app is simply not needed, and, frankly, it leads to possible hours of attempts like โ€œto go down the wrong roadโ€, adding and removing it all over the code (if you use :)

 import android.support.v4.app.FragmentTransaction; 

In this example, you do not need to import FragmentManager support. However, if you get errors, make sure that you yourself have imported the library into your "libs" folder.

This solution will fix the problem of overlapping fragment and, I hope, will save hours of hours on replacing fragments.

+4
May 08 '13 at 22:46
source share

Well, I ran into the same problem, and I just replaced the snippet from the main layout with a linear layout and guessed that it worked .. its weird doesn't know how its work. I use the action bar to switch between fragments to replace my code:

  protected class MyTabsListener1 implements ActionBar.TabListener{ private Fragment frag ; public MyTabsListener1 ( Fragment frag){ this.frag = frag; } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { switch (tab.getPosition()){ case 0: ft.replace(R.id.replace, homeFrag); break; case 1: ft.replace(R.id.replace, createFrag); break; case 2: ft.replace(R.id.replace, ioListFrag); break; case 3: ft.replace(R.id.replace, settingFrag); break; default: break; } } 

and my main layout is this:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:id="@+id/replace" android:layout_width="wrap_content" android:layout_height="match_parent" > </LinearLayout> </LinearLayout> 
+2
Aug 01 '12 at 7:00
source share

In short, you CANNOT replace a fragment if u defined it in XML with a fragment tag. Instead, as @pawelzieba advised adding a frame tag to your layout, find it and add, delete, replace fragments there .. I hope this helps. Greetings

+1
Aug 03 2018-12-12T00:
source share

The main advantage of using fragments is to force them to process parts of the screen, not the entire screen, which is what actions do.

If you just create an application for a small screen that will function as an Activity but encoded in Fragments, just create a separate FragmentActivity for each of your fragments.

Do this onCreate of your FragmentActivity:

 public void onCreate(Bundle savedInstanceState) { setContentView(R.layout.emptylayout); FragmentManager fragmentManager = getSupportFragmentManager(); //Or getFragmentManager() if you're not using the support library FragmentTransaction fragmentTransaction = fragmentManager .beginTransaction(); YourFragment fragment = new YourFragment(); fragmentTransaction.add(R.id.emptyview, fragment); fragmentTransaction.commit(); } 

Where layout / emptylayout is the XML layout file with FrameLayout. id / emptyview is FrameLayout.

If you want to use XML for the actual fragment layout, make a separate XML layout for the actual fragment and inflate it in the `onCreateView 'fragment:

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.files, container, false); // Do stuff here return view; } 

Then just use startActivity(new Intent(getActivity(), YourFragmentActivity.class)) to start the FragmentActivity from the fragment.

It seems redundant, yes, but if you plan to focus more on larger screens (if not, why are you worried about fragments?), This will make it easier in the long run.

0
Jul 28 2018-12-12T00:
source share



All Articles