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.
AndroidStudio ver2.2.2
FragmentArgs ver3.0.2
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.
For the installation method, I referred to the git page. To install FragmentArgs, add the following content.
build.gradle(Project)
dependencies{
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7'
}
allprojects {
repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
}
build.gradle(app)
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'
}
FragmentArgs.inject (this) </ code>.
@Arg </ code>.
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 **.
--Declare a variable (userName, age this time) to store the received value. FragmentArgs will generate a builder class from the fields below.
@Arg
String userName;
@Arg
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)
FragmentArgs.inject(this)
The above code corresponds to getArgments () </ code> and is assigned to the variable with
@Arg </ code>.
In summary, the code is as follows.
@FragmentWithArgs </ code> in front of the class name.
myFragment.java
@FragmentWithArgs
public class myFragment extends Fragment {
@Arg
String userName;
@Arg
int age;
public myFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentArgs.inject(this);
}
Tips
The description of Gradle may be incorrect. Let's check the description location and description content (ver etc.) again.
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].
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. ..
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