Skip to content

Instantly share code, notes, and snippets.

@morristech
Forked from keyboardr/HeadlessFragment.java
Created March 8, 2019 05:32
Show Gist options
  • Save morristech/ca16bfd907b0c63d13e88ec710f26b1a to your computer and use it in GitHub Desktop.
Save morristech/ca16bfd907b0c63d13e88ec710f26b1a to your computer and use it in GitHub Desktop.

Revisions

  1. @keyboardr keyboardr revised this gist Jun 7, 2015. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    Copyright 2015 Joshua Brown

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
  2. @keyboardr keyboardr revised this gist Jun 7, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion HeadlessFragment.java
    Original file line number Diff line number Diff line change
    @@ -42,4 +42,4 @@ public interface ${PARENT_INTERFACE} {
    }
    return null;
    }
    }
    }
  3. @keyboardr keyboardr revised this gist Jun 7, 2015. 1 changed file with 11 additions and 17 deletions.
    28 changes: 11 additions & 17 deletions HeadlessFragment.java
    Original file line number Diff line number Diff line change
    @@ -7,19 +7,17 @@
    #parse("File Header.java")
    public class ${NAME} extends Fragment {

    private static final String FRAG_TAG = ${NAME}.class.getCanonicalName();

    public interface ${PARENT_INTERFACE} {
    //TODO: define methods for parent interface
    }

    public static <ParentFrag extends Fragment & ${PARENT_INTERFACE}> ${NAME}
    attach(ParentFrag parent) {
    return attach(parent.getChildFragmentManager());
    }

    public static <ParentActivity extends Activity & ${PARENT_INTERFACE}> ${NAME}
    attach(ParentActivity parent) {
    private static final String FRAG_TAG = ${NAME}.class.getCanonicalName();

    public interface ${PARENT_INTERFACE} {
    //TODO: define methods for parent interface
    }

    public static <ParentFrag extends Fragment & ${PARENT_INTERFACE}> ${NAME} attach(ParentFrag parent) {
    return attach(parent.getChildFragmentManager());
    }

    public static <ParentActivity extends Activity & ${PARENT_INTERFACE}> ${NAME} attach(ParentActivity parent) {
    return attach(parent.getFragmentManager());
    }

    @@ -44,8 +42,4 @@ public interface ${PARENT_INTERFACE} {
    }
    return null;
    }

    public ${NAME}() {
    // leave empty
    }
    }
  4. @keyboardr keyboardr created this gist May 30, 2015.
    51 changes: 51 additions & 0 deletions HeadlessFragment.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    #if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

    import android.app.Activity;
    import android.app.Fragment;
    import android.app.FragmentManager;

    #parse("File Header.java")
    public class ${NAME} extends Fragment {

    private static final String FRAG_TAG = ${NAME}.class.getCanonicalName();

    public interface ${PARENT_INTERFACE} {
    //TODO: define methods for parent interface
    }

    public static <ParentFrag extends Fragment & ${PARENT_INTERFACE}> ${NAME}
    attach(ParentFrag parent) {
    return attach(parent.getChildFragmentManager());
    }

    public static <ParentActivity extends Activity & ${PARENT_INTERFACE}> ${NAME}
    attach(ParentActivity parent) {
    return attach(parent.getFragmentManager());
    }

    private static ${NAME} attach(FragmentManager fragmentManager) {
    ${NAME} frag = (${NAME}) fragmentManager.findFragmentByTag(FRAG_TAG);
    if (frag == null) {
    frag = new ${NAME}();
    fragmentManager.beginTransaction().add(frag, FRAG_TAG).commit();
    }
    return frag;
    }

    private ${PARENT_INTERFACE} getParent() {
    Fragment parentFragment = getParentFragment();
    if (parentFragment instanceof ${PARENT_INTERFACE}) {
    return (${PARENT_INTERFACE}) parentFragment;
    } else {
    Activity activity = getActivity();
    if (activity instanceof ${PARENT_INTERFACE}) {
    return (${PARENT_INTERFACE}) activity;
    }
    }
    return null;
    }

    public ${NAME}() {
    // leave empty
    }
    }