啟動Activity并傳遞參數(shù)
Extra
正常情況下啟動Activity并且傳遞參數(shù)的代碼:
Intent intent = new Intent(context,LoginActivity.class);
intent.putExtra("phone","123456);
intent.putExtra("pwd","123456);
startActivity(intent);
使用Blade啟動Activity的方式
public class LoginActivity extends AppcompatActivity{
@Extra
String mText;
@Extra
MyData mData;
}
通過上面的代碼就會自動生成一個如下兩個方法
Intent forX(Context c, T1 extra1[, T2 extra2, ...])
void startX(Context c, T1 extra1[, T2 extra2, ...])
然后我們就可以直接通過I.startLoginActivity來啟動Activity。
創(chuàng)建Fragment實(shí)例
@Arg
用來為Fragment生成newInstance方法
通常我們創(chuàng)建Fragment對象都是些如下的樣板代碼
public class MyFragment extends Fragment{
public MyFragment newInstance(String data){
MyFragment fragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putExtra("data",data);
fragment.setArguments(bundle);
return fragment;
}
...
}
使用Blade的方式
public class MyFragment extends Fragment {
@Arg
String mText;
@Arg
MyData mData;
}
自定義序列化
public class MyFragment extends Fragment {
@Arg(MyTypeBundler.class)
MyType mMyType;
}
public class MyTypeBundler implements Bundler<MyType> {
void save(@Nullable final MyType value, @NonNull final Bundle state) {
// save given value to the state
}
@Nullable
MyType restore(@NonNull final Bundle state) {
// restore and return value from state
}
}
@Parcel
當(dāng)我們創(chuàng)建一個實(shí)體類需要實(shí)現(xiàn)Parcelable的時候,可以按如下的方法寫
@blade.Parcel
public class MyClass implements Parcelable {
String text;
int number;
boolean flag;
double[] doubleArray;
protected MyClass(Parcel in) {
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
}
如果某個字段想忽略,不需要被序列化的話,使用@blade.ParcelIgnore
Mvp
Mvp是和Dager配合使用的。
第一步:在你的build.gradle中添加dager依賴
compile 'com.google.dagger:dagger:2.x'
apt 'com.google.dagger:dagger-compiler:2.x'
第二步:創(chuàng)建一個繼承自IView的接口
public interface IMyView extends blade.mvp.IView {
void show(String something);
}
第三步:創(chuàng)建Prensenter和View接口相互影響
public class MyPresenter extends blade.mvp.BasePresenter<IMyView> {
public void onUserDidSomething() {
String s = // do something ...
if (getView() != null) {
getView().show(s);
}
}
//...
}
第四步:創(chuàng)建View的實(shí)現(xiàn),使用@Inject注入Presenter,現(xiàn)在支持Fragmnt,Activit,View
public class MyView extends android.support.v4.app.Fragment implements IMyView {
@Inject
MyPresenter mPresenter;
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
yourDaggerComponent.inject(this);
}
@Override
void show(String something) { /* ... */ }
// ...
}
第五步:Activity中包含存在Presenter的Fragment,View,那么該Activity需要使用@Blade注解一遍讓Blade知道。
State
簡化狀態(tài)管理,@State注解會生成一個幫助類,里面包含兩個靜態(tài)的方法:
public class StateArgFragment extends Fragment {
@Arg
@State
int num;
}
@Weave(
into = "0_onSaveInstanceState",
args = {"android.os.Bundle"},
statement = "eu.f3rog.blade.sample.state.StateArgFragment_Helper.saveState(this, $1);"
)
public static void saveState(StateArgFragment target, Bundle state) {
if (state == null) {
throw new IllegalArgumentException("State cannot be null!");
}
BundleWrapper bundleWrapper = BundleWrapper.from(state);
bundleWrapper.put("<Stateful-num>", target.num);
}
@Weave(
into = "1^onCreate",
args = {"android.os.Bundle"},
statement = "eu.f3rog.blade.sample.state.StateArgFragment_Helper.restoreState(this, $1);"
)
public static void restoreState(StateArgFragment target, Bundle state) {
if (state == null) {
return;
}
BundleWrapper bundleWrapper = BundleWrapper.from(state);
target.num = bundleWrapper.get("<Stateful-num>", target.num);
}
繼承自Fragment、Activity或View的類都會自動管理狀態(tài)。自定義序列化的功能如上所示。
Blade可以讓我們少寫很多的樣板代碼,具體的我還沒有應(yīng)用到項(xiàng)目中,之后會在項(xiàng)目中進(jìn)行使用,用來使項(xiàng)目看起來更加清晰。
Blade地址:https://github.com/FrantisekGazo/Blade