The child FragmentManager is the one that handles Fragments contained within only the Fragment that it was added to. The other FragmentManager is contained within the entire Activity .
To remove (so destroy) these fragments, you can call: You need to use findFragmentById for Fragment from XML file. And then find this in the MainActivity on click listener. I've added this to the OnClickListener but it always returns 'null' for the fragment I want to remove.
Fragments introduce modularity and reusability into your activity's UI by allowing you to divide the UI into discrete chunks. Activities are an ideal place to put global elements around your app's user interface, such as a navigation drawer.
Finally! the android 4.2 update comes with default support for nested fragments, also compatible: Official NestedFragments. What's more, the support of the nested Fragments in announced in the latest 11th revision of the v4 support library!
No, fragments are currently NOT in a hierarchy. Trying to have one fragment embedded in another can lead to problems, often subtle.
Edit: As of ACLv11 this is only needed if you replace(...)
ViewPagers
dynamically.
Yes you can, but there are certain ways that are a pain, but I have even embedded FragmentViewPagers.
If you embed in XML then when the layout is inflated you will have no problems.
If you embed fragments dynamically within another fragment then you will get issues! Although it is possible.
Generally involves posting to a handler which then executes after the current fragment transition then starts another, this way you can embed fragments pretty easily. But this leads to other issues, so you have to be careful how you implement this.
FoodTestFeast has embedded ViewPagers which are removed and added dynamically.
My Base FragmentViewPager - Fragment that holds a view pager (Which has FragmentPagerAdapter)
I haven't seen any crashes using this yet, but let me know if anyone else does.
/**
* @project FoodFeast
* @author chris.jenkins
* @created Dec 28, 2011
*/
package couk.jenxsolution.food.ui.fragments;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.viewpagerindicator.TabPageIndicator;
import couk.jenxsolution.food.R;
import couk.jenxsolution.food.app.FoodApplication;
/**
* @author chris.jenkins
*/
public class QuizPagerBaseFragment extends QuizBaseFragment
{
// Data
private FragmentStatePagerAdapter mAdapter = null;
// Views
private int layoutRes;
private View mRootView;
private ViewPager mPager;
private TabPageIndicator mIndicator;
// Handlers
private final Handler handler = new Handler();
private Runnable runPager;
private boolean mCreated = false;
/**
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
* android.view.ViewGroup, android.os.Bundle)
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
mRootView = inflater.inflate(layoutRes, container, false);
mPager = (ViewPager) mRootView.findViewById(R.id.pager);
mPager.setOffscreenPageLimit(2);
mIndicator = (TabPageIndicator) mRootView.findViewById(R.id.indicator);
mIndicator.setTypeface(FoodApplication.getInstance().getTFs().getTypeFaceBold());
return mRootView;
}
/**
* @see android.support.v4.app.Fragment#onActivityCreated(android.os.Bundle)
*/
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
if (runPager != null) handler.post(runPager);
mCreated = true;
}
/**
* @see android.support.v4.app.Fragment#onPause()
*/
@Override
public void onPause()
{
super.onPause();
handler.removeCallbacks(runPager);
}
/**
* Set the ViewPager adapter from this or from a subclass.
*
* @author chris.jenkins
* @param adapter
*/
protected void setAdapter(FragmentStatePagerAdapter adapter)
{
mAdapter = adapter;
// mAdapter.notifyDataSetChanged();
runPager = new Runnable() {
@Override
public void run()
{
mPager.setAdapter(mAdapter);
mIndicator.setViewPager(mPager);
// mAdapter.notifyDataSetChanged();
mIndicator.setVisibility(View.VISIBLE);
}
};
if (mCreated)
{
handler.post(runPager);
}
}
/**
* Has to be set before onCreateView
*
* @author chris.jenkins
* @param layoutRes
*/
protected void setLayoutRes(int layoutRes)
{
this.layoutRes = layoutRes;
}
}
FYI your layout will need to look like this (or similar): NOTE THE Indicator = GONE!
<?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="vertical" >
<com.viewpagerindicator.TabPageIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
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