The standard way to declare fragments in a xml layout file is
<LinearLayout ...> <fragment class="com.example.SomeFragment" </LinearLayout>
where SomeFragment is a java class defined like
class SomeFragment extends Fragment { ... }
Lets say, I have 3 fragments; fragment1, fragment2, and fragment3. When the user launches the app, I show them fragment1, and when they click on a button, I replace the fragment1 with fragment2, etc.
What is the best approach to define the 3 fragments in a single layout xml file?
Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();
Fragments cannot live on their own--they must be hosted by an activity or another fragment. The fragment's view hierarchy becomes part of, or attaches to, the host's view hierarchy.
onStart() makes the fragment visible to the user (based on its containing activity being started). onResume() makes the fragment begin interacting with the user (based on its containing activity being resumed).
Yes you can! Show activity on this post.
You should use a FrameLayout for that, that way you don't have to specify the fragment class in the XML and that way it is not limited to one class.
<FrameLayout android:id="@+id/contentFragment" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" />
and than you can set the fragment in the code like this
Fragment fragment = new YourFragment(); FragmentManager fm = getSupportFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); transaction.replace(R.id.contentFragment, fragment); transaction.commit();
I am giving an example to switch between two layouts in a fragments:
First declare a layout with two fragments:(it depends on how many fragments you want in your layout)
fragment_layout_example.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <FrameLayout android:id="@+id/fragment_container" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/Fragment2" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" class="com.example.SecondFragment" > <!-- Preview: layout=@layout/details --> </fragment> </LinearLayout>
Above layout will display two fragments Fragment1 and Fragment2. For Fragment1 I had declared the container as content of the container is going to changed at runtime. So not declared the Fragment
class here. for more on this check
http://developer.android.com/training/basics/fragments/fragment-ui.html
Then create a class FragmentExampleActivity which extends Activity
. In case you are using Fragment in Backward compatibility mode then extend FragmentActivity
public class FragmentExampleActivity extends FragmentActivity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_layout_example); // Check that the activity is using the layout version with // the fragment_container FrameLayout if (findViewById(R.id.fragment_container) != null) { // However, if we're being restored from a previous state, // then we don't need to do anything and should return or else // we could end up with overlapping fragments. if (savedInstanceState != null) { return; } // Create an instance of Fragment1 Fragment1 firstFragment = new Fragment1(); // In case this activity was started with special instructions from an Intent, // pass the Intent's extras to the fragment as arguments firstFragment.setArguments(getIntent().getExtras()); // Add the fragment to the 'fragment_container' FrameLayout getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, firstFragment).commit(); } } }
To create a layout for two fragments create two classes which extends Fragment
public class Fagment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //set the layout you want to display in First Fragment View view = inflater.inflate(R.layout.fragment1, container, false); return view; } }
Same way create Fragment class for second Fragment and set the layout
Now If you want to switch your fragment layout in Fragment1 to another layout on click of a button then create another class say Fragment3.java and set the layout you want to switch and write the below code inside the Fragment1.java
@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Button showFragment3=(Button)getView().findViewById(R.id.Button1); showFragment3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager .beginTransaction(); Fragment3 fragment3 = new Fragment3(); fragmentTransaction.replace(R.id.Fragment1, fragment3); //provide the fragment ID of your first fragment which you have given in //fragment_layout_example.xml file in place of first argument fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } }); }
Now to come back again on the first Fragment you can click on back button. But if you want to come back on click of button then write the below code in Fragment3.java
@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Button showFragment1 = (Button) getView().findViewById( R.id.Button2); showFragment1 .setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { getFragmentManager().popBackStack(); } }); }
Thanks! Hope it will help you...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With