AutoBundle generates boilerplate code for field binding with android.os.Bundle
.
AutoBundle supports these classes.
Activity
Fragment
BroadcastReceiver
Service
Here is example for Fragment. First, declare the field with @AutoBundleField
.
public class ExampleFragment extends Fragment {
// field with @AutoBundleField, must not be private/protected.
@AutoBundleField
String title;
@AutoBundleField
int exampleId;
@AutoBundleField(required = false) // default is true
int optionalId;
}
In caller section, you can use generated builder.
ExampleFragment fragment = ExampleFragmentAutoBundle
.createFragmentBuilder("hello, example!", 1)
.optionalId(2) // here is optional
.build();
Helper class is named {YourClass}AutoBundle
.
FragmentBuilder also has method build(Fragment fragment)
,
so you can set bundle to existing instance.
For Intent
, here is builder example.
Intent intent = ExampleActivityAutoBundle.createIntentBuilder("hello, example!")
.optionalName("optionalName")
.fooList(messages)
.build(this)
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Builder class has both methods build(Context context)
, build(Intent intent)
.
In target class, Call binding method in onCreate
.
public class ExampleFragment extends DialogFragment {
// field with @AutoBundleField, must not be private/protected.
@AutoBundleField
String title;
@AutoBundleField
int exampleId;
@AutoBundleField(required = false) // default is true
int optionalId;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ExampleFragmentAutoBundle.bind(this, getArguments());
// `AutoBundle` is providing easier interface.
// This code is equals to above.
AutoBundle.bind(this);
}
}
bind(Object target, Intent intent)
bind(Object target, Bundle bundle)
bind(Activity target)
(equals tobind(activity, activity.getIntent())
)bind(Object target)
(equals tobind(fragment, fragment.getArguments())
)
AutoBundle bindings are also useful as restoring value in onSaveInstanceState(Bundle outState)
.
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
ExampleFragmentAutoBundle.pack(this, outState);
}
pack(Object object, Bundle bundle)
stores field value to bundle.
For example, store in onSaveInstanceState
and restore in onCreate
.
key
is key for Bundle
. Default is field name.
You cannot define duplicate key in one class.
@AutoBundleField(key = "exampleId")
int id;
required
option is true by default.
If false
, Builder class has method which named key name, instead as contructor argument.
@AutoBundleField(required = false)
int optionalId;
then,
ExampleFragment fragment = ExampleFragmentAutoBundle
.createFragmentBuilder()
.optionalId(2)
.build();
You can use getter/setter for fields.
The method named get/set{key}
As a default,
But you can specify with @AutoBundleGetter
and @AutoBundleSetter
.
@AutoBundleField
private String userId;
// get{key} use as default.
// no need for @AutoBundleGetter
public String getUserId() {
return userId;
}
@AutoBundleGetter(key = "userId")
public String getId() {
return userId;
}
@AutoBundleSetter(key = "userId")
public void setId(String id) {
this.userId = id;
}
converter
option provide custom converter for storing to bundle.
You can specify class which implements AutoBundleConverter<T, U>
.
public class ExampleFragment extends Fragment {
@AutoBundleField(converter = DateArgConverter.class)
Date targetDate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ExampleFragmentAutoBundle.bind(this);
}
public static class DateArgConverter implements AutoBundleConverter<Date, Long> {
@Override
public Long convert(Date o) {
return o.getTime();
}
@Override
public Date original(Long s) {
return new Date(s);
}
}
}
For more information or usage, see the sample application!
Both Fragment and Intent are able to pass value by storing Bundle.
There is a good pattern, known as "createIntent" or "newInstance". Static method in called class (e.g. Fragment, Activity) promised the expected data will be passed. AutBundle provide this pattern as builder method instead of "createIntent".
AutoBundle uses annotation processing, so you need to add android-apt plugin.
buildscript {
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
apply plugin: 'android-apt'
dependencies {
compile 'com.github.yatatsu:autobundle:3.1.1'
apt 'com.github.yatatsu:autobundle-processor:3.1.1'
}
Actually, AutoBundle is that just integrates above both great libraries.
Copyright 2015 KITAGAWA, Tatsuya
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.