Add a Fragment Dinamically
- The general steps are
- While the activity is running can add a fragment to the layout
- Get reference to the FragmentManager
- Begin a FragmentTransaction
- Add the Fragment
- Commit the FragmentTransaction
- To change the previous application to add fragments dynamically, so:
- In the MainActivity.java override the onCreate method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTitleArray = getResources().getStringArray(R.array.Titles);
mQuoteArray = getResources().getStringArray(R.array.Quotes);
setContentView(R.layout.main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.title_frame, new TitlesFragment());
fragmentTransaction.add(R.id.quote_frame, mQuoteFragment);
fragmentTransaction.commit();
}
- In the mail.xml file change the tag <fragment> per FragmentLayout
<FrameLayout
android:id="@+id/title_frame"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
<FrameLayout
android:id="@+id/quote_frame"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" >
</FrameLayout>
How to add or remove Fragments
In this case at the beginning only one fragment will appear and after selection will appear the detail. If click back button will appear only one fragment.
- Override the onCreate method
- Create listener when push the BackButton
R.id.title_fragment_container - Is the first FrameLayout from main.xml
new TitlesFragment() - Is the class related with the FrameLayout
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager mFragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.title_fragment_container,
new TitlesFragment());
fragmentTransaction.commit();
mFragmentManager
.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
public void onBackStackChanged() {
setLayout();
}
});
}
- setLayout method definition
- If the mQuoteFragment is not added the width is 0
Are the FrameLayouts defined in the main.xml. Initialized onCreated method.
mTitleFrameLayout = (FrameLayout) findViewById(R.id.title_fragment_container);
mQuotesFrameLayout = (FrameLayout) findViewById(R.id.quote_fragment_container);
Global variable
private final QuotesFragment mQuoteFragment = new QuotesFragment();
if (!mQuoteFragment.isAdded()) {
mTitleFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(
MATCH_PARENT, MATCH_PARENT));
mQuotesFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(0,
MATCH_PARENT));
} else {
mTitleFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(0,
MATCH_PARENT, 1f));
mQuotesFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(0,
MATCH_PARENT, 2f));
}
- If do click in the menu option, the detail fragment is added
@Override
public void onListSelection(int index) {
if (!mQuoteFragment.isAdded()) {
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.quote_fragment_container,
mQuoteFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
mFragmentManager.executePendingTransactions();
}
if (mQuoteFragment.getShownIndex() != index) {
mQuoteFragment.showIndex(index);
}
}
Configuration Changes
How to avoid delete the fragment
- On the method onCreate, use setRetainInstance method in the Fragment class
- This method avoid to call the methods onDestroy() and onCreate()
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
Comentarios
Publicar un comentario