Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Turns out <code>FragmentTransaction.replace()</code> is the correct operation and should work correctly. It only does not work when using <code>ActionBarSherlock</code> and <code>SherlockFragmentActivity</code> so I can only assume it is a bug in this compatibility library.</p> <p>I have confirmed this through using the code below to implement the desired behaviour through Android on API11+, the android compatibility library, and ActionBarSherlock. It only breaks in the last instance.</p> <pre><code>package com.test.test; import com.actionbarsherlock.app.SherlockFragmentActivity; import android.os.Bundle; import android.content.Context; import android.graphics.Color; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.TextView; public class MainActivity extends SherlockFragmentActivity { // Consistent fragment instance myFragment myFrag = null; // Views FrameLayout fl = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); Button b = new Button(this); b.setText("Repeat"); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Reattach the same fragment getSupportFragmentManager().beginTransaction().replace(fl.getId(), myFrag).commit(); } }); fl = new FrameLayout(this); fl.setId(200); fl.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); myFrag = new myFragment(); getSupportFragmentManager().beginTransaction().add(fl.getId(), myFrag).commit(); ll.addView(b); ll.addView(fl); setContentView(ll); } public static class myFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView tv = new TextView(getActivity()); tv.setText("My fragment"); tv.setGravity(Gravity.CENTER); tv.setBackgroundColor(Color.RED); return tv; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload