@Retention(value=CLASS) @Target(value={FIELD,METHOD,PARAMETER}) public @interface FragmentByTag
Use it on android.app.Fragment or android.support.v4.app.Fragment fields or methods with applicable parameters in activity classes to retrieve and inject a fragment.
The annotation value should be one of fragment tag. If not set, the field or method name will be used as the tag name.
Note: This can only inject an existing fragment, not create them.
Example :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/myFragment"
android:tag="myFragmentTag"
android:name="mypackage.MyFragment_"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
@EActivity(R.layout.main)
public class MyActivity extends Activity {
// all injected fragment will be the same
@FragmentByTag
public MyFragment myFragmentTag;
@FragmentByTag("myFragmentTag")
public MyFragment myFragmentTag2;
@FragmentByTag
void singleInjection(MyFragment myFragmentTag) {
// do stuff
}
void multiInjection(@FragmentByTag MyFragment myFragmentTag, @FragmentById("myFragmentTag") MyFragment myFragment2) {
// do stuff
}
}
To use the getChildFragmentManager() to inject the
Fragment, set the childFragment() annotation parameter
to true. You can only do this if the annotated field is in a
class which extends android.app.Fragment or
android.support.v4.app.Fragment and the
getChildFragmentManager() method is available.
Example :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/myChildFragment"
android:tag="myChildFragment"
android:name="mypackage.MyChildFragment_"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
@EFragment(R.layout.parentfragment)
public class MyParentFragment extends Fragment {
@FragmentByTag(childFragment = true)
MyChildFragment myFragment;
}
EFragment,
FragmentArg,
FragmentById| Modifier and Type | Optional Element and Description |
|---|---|
boolean |
childFragment
Whether to use
getChildFragmentManager() or
getFragmentManager() to obtain the Fragment. |
String |
value
The tag of the injected Fragment.
|
public abstract String value
Copyright © 2010–2020 simpligility technologies inc.. All rights reserved.