Skip to content

Instantly share code, notes, and snippets.

@nseidm1
Last active August 29, 2015 13:56
Show Gist options
  • Save nseidm1/9252142 to your computer and use it in GitHub Desktop.
Save nseidm1/9252142 to your computer and use it in GitHub Desktop.
The ChildConsiderateFragment allows fragments to be defined in it's xml layout. The child fragments will be determined in the onCreateView callback by parsing the xml layout returned by the abstract getLayoutId method. The child fragments will automatically be cleaned up in the onDestroyView callback.
package com.paltalk.chat.utils;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* The ChildConsiderateFragment allows fragments to be defined in it's xml layout. The child fragments will be determined in the onCreateView
* callback by parsing the xml layout returned by the abstract getLayoutId method. The child fragments will automatically be cleaned up
* in the onDestroyView callback.
* @author Noah Seidman
*/
public abstract class ChildConsiderateFragment extends Fragment {
public abstract int getLayoutId();
private ArrayList<Fragment> mChildFragments = new ArrayList<Fragment>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
findChildFragments();
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for (Fragment fragment : mChildFragments)
fragmentTransaction.remove(fragment);
fragmentTransaction.commitAllowingStateLoss();
}
/**
* This method requires your xml child fragments to have an android:id or android:tag attribute defined. The tag
* takes priority over the id value.
*/
private void findChildFragments() {
try {
Log.d(ChildConsiderateFragment.class.getSimpleName(), "findChildFragments()");
XmlResourceParser parser = getActivity().getResources().getLayout(getLayoutId());
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
Log.d(ChildConsiderateFragment.class.getSimpleName(), "Tag: " + parser.getName());
if(eventType == XmlPullParser.START_TAG && parser.getName().equals("fragment")) {
String sResourceId = parser.getAttributeValue("http://schemas.android.com/apk/res/android", "tag");
if (sResourceId == null) {
int iResourceId = parser.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "id", 0);
Log.d(ChildConsiderateFragment.class.getSimpleName(), "Resource ID: " + iResourceId);
mChildFragments.add(getFragmentManager().findFragmentById(iResourceId));
} else {
Log.d(ChildConsiderateFragment.class.getSimpleName(), "Resource Tag: " + sResourceId);
mChildFragments.add(getFragmentManager().findFragmentByTag(sResourceId));
}
}
eventType = parser.next();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
@7frawy
Copy link

7frawy commented Mar 24, 2014

"created_on": "2012-12-07T18:11:55Z"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment