[JAVA] Summary of using FragmentArgs


I learned a useful library for implementing an Android application, so I will summarize how to use it. ** Contents will be updated sequentially **.

The development environment is as follows.

What is Fragment Args?

When trying to implement an app, you may want to pass arguments at the time of screen transition. Normally, it is stored in Intent or Bundle in the form of "Key: value" and passed when transitioning. ** As the number of screens and arguments increase, the code becomes longer and less readable **. ~~ In the first place, if the amount of code increases, various mistakes will occur ... ~~

Fragment Args plays an active role in such situations. ** FragmentArgs automatically generates a builder class from Fragment, Simplify data exchange between Fragments ** Will do it.

How to install Fragment Args

For the installation method, I referred to the git page. To install FragmentArgs, add the following content.


    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7'  

allprojects {
    repositories {
        maven {  url "https://oss.sonatype.org/content/repositories/snapshots/"  }


apply plugin: 'com.neenbedankt.android-apt'

dependencies {
    compile 'com.hannesdorfmann.fragmentargs:annotation:3.0.2'
    compile 'com.hannesdorfmann.fragmentargs:bundler-parceler:3.0.2'
    apt 'com.hannesdorfmann.fragmentargs:processor:3.0.2'

How to use FragmentArgs

Rough processing flow

  1. (Caller) Generate a transition destination using the Builder class. At this time, the value to be passed is used as an argument.
  2. (Callee) Get the value passed in FragmentArgs.inject (this) </ code>.
  3. (Callee) Store the acquired value in the variable with @Arg </ code>.

How to pass a value (when passing userName, age to MyFragment)

   MyFragment myFragment = new MyFragmentBuilder(userName,age).build();

MyFragmentBuilder is a class automatically generated by FragmentArgs. It is generated by doing [Run: app], so let's run it without worrying even if there is an error at first. If the ** builder is not generated, try the method described in Tips **.

How to receive the value

--Declare a variable (userName, age this time) to store the received value. FragmentArgs will generate a builder class from the fields below.

	String userName;
	int age;

At this time, if you write @Arg (required = false) </ code>, it can be an optional argument. You can also add private </ code> to the variable. In that case, don't forget to prepare setter, getter </ code>.

--Store in the declared variable (described in OnCreate)


The above code corresponds to getArgments () </ code> and is assigned to the variable with @Arg </ code>.

In summary, the code is as follows.

  • At this time, do not forget to add @FragmentWithArgs </ code> in front of the class name.


public class myFragment extends Fragment {

    String userName;

    int age;

    public myFragment() {

    public void onCreate(Bundle savedInstanceState) {


What to do if the builder is not automatically generated

The description of Gradle may be incorrect. Let's check the description location and description content (ver etc.) again.

  • Be careful of typographical errors in apt and annotationParser.

If the description is correct, it may work by cleaning the project. For Android Studio: You can clean the project by selecting [Menu] → [build] → [clean Project].

Argument order when passing values

Basically, the constructors are generated in the order declared in Fragment. Make sure that the order of the arguments in the builder matches the order of the variables with Arg </ code> in the Fragment.

For some reason my code is built out of order so I'm investigating the cause. ..

When using DialogFragment

If you want to pass a value to a class that inherits DialogFragment, write FragmentArgs.inject (this) </ code> in OnCreateDialog () </ code>. This passes activity, so replace this </ code> with getActivity () </ code> as needed.

Recommended Posts