Last active
December 31, 2021 01:05
-
-
Save keyboardr/ddf35148ca2c1a2bfbde to your computer and use it in GitHub Desktop.
A file template for creating a headless Fragment. The attach() methods add the fragment to the parent if it doesn't already exist and returns the attached fragment. The methods ensure that the parent implements the parent interface. If needed, parameters may be added to the attach() methods to supply fragment arguments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
amazing idea!!!,How you can think that!!! Is lots of fragment can affect activity performance?