Android replace the current fragment with another fragment
-
I just started with fragment design for HoneyComb. I created two fragments. When i click a button in the left side fragment, a new fragment is created in right side. Meanwhile when i click a button in the right fragment(ie. DetialsFragment in my code below should be replaced by another fragment. main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <fragment class="com.fragment.example.Titles" android:id="@+id/titles" android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent" /> <FrameLayout android:id="@+id/details" android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent" /> </LinearLayout> FragmentExample.java public class FragmentExample extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } Titles.java public class Titles extends Fragment { public FragmentTransaction ft; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.main1, null); Button button1 = (Button)v.findViewById(R.id.button1); button1.setText("santhosh"); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub DetailsFragment details = (DetailsFragment) getFragmentManager().findFragmentById(R.id.details); if (details == null || details.getShownIndex() != 1) { // Make new fragment to show this selection. details = DetailsFragment.newInstance(1); // Execute a transaction, replacing any existing // fragment with this one inside the frame. ft = getFragmentManager().beginTransaction(); ft.add(R.id.details, details, "detail"); ft.setTransition( FragmentTransaction.TRANSIT_FRAGMENT_FADE); ft.commit(); } } }); return v; } } DetailsFragment.java public class DetailsFragment extends Fragment { /** * Create a new instance of DetailsFragment, initialized to * show the text at 'index'. */ Titles title = new Titles(); String[] titles = {"Title1", "Title2", "Title3", "Title4"}; public static DetailsFragment newInstance(int index) { DetailsFragment f = new DetailsFragment(); // Supply index input as an argument. Bundle args = new Bundle(); args.putInt("index", index); f.setArguments(args); return f; } public int getShownIndex() { return getArguments().getInt("index", 0); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { // Currently in a layout without a container, so no // reason to create our view. return null; } Button button = new Button(getActivity()); button.setText("Next"); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } }); return button; } }
-
Answer:
Then provided your button is showing and the click event is being fired you can call the following in your click event: final FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.details, new NewFragmentToReplace, "NewFragmentTag"); ft.commit(); and if you want to go back to the DetailsFragment on clicking back ensure you add the above transaction to the back stack, i.e. ft.addToBackStack(null); Or am I missing something? Alternatively some people suggest that your activity gets the click event for the button and it has responsibility for replacing the fragments in your details pane.
Santhosh_pulliman at Stack Overflow Visit the source
Related Q & A:
- How to call Fragment from another Fragment?Best solution by Stack Overflow
- How to open current activity with android notification?Best solution by Stack Overflow
- How to remove fragment from backstack Android?Best solution by Stack Overflow
- How to replace a fragment of view pager with other new fragment in android?Best solution by Stack Overflow
- How to send data from Fragment to an Activity in Android?Best solution by forum.xda-developers.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.