Start the Activity and pass the parameters
Extra
Under normal circumstances, the code to start the Activity and pass parameters:
Intent intent = new Intent(context,); ("phone","123456); ("pwd","123456); startActivity(intent);
How to start Activity with Blade
public class LoginActivity extends AppcompatActivity{ @Extra String mText; @Extra MyData mData; }
Through the above code, the following two methods will be automatically generated
Intent forX(Context c, T1 extra1[, T2 extra2, ...]) void startX(Context c, T1 extra1[, T2 extra2, ...])
Then we can start the Activity directly through .
Create a Fragment instance
@Arg
Used to generate newInstance method for Fragment
Usually, we create Fragment objects with the following boilerplate code
public class MyFragment extends Fragment{ public MyFragment newInstance(String data){ MyFragment fragment = new MyFragment(); Bundle bundle = new Bundle(); ("data",data); (bundle); return fragment; } ... }
How to use Blade
public class MyFragment extends Fragment { @Arg String mText; @Arg MyData mData; }
Custom serialization
public class MyFragment extends Fragment { @Arg() 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
When we create an entity class and need to implement Parcelable, we can write it as follows
@ 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) { } }
If a field wants to be ignored and does not need to be serialized, use @
Mvp
Mvp is used in conjunction with Dager.
Step 1: Add dager dependencies in your
compile ':dagger:' apt ':dagger-compiler:'
Step 2: Create an interface inherited from IView
public interface IMyView extends { void show(String something); }
Step 3: Create Prensenter and View interfaces to influence each other
public class MyPresenter extends <IMyView> { public void onUserDidSomething() { String s = // do something ... if (getView() != null) { getView().show(s); } } //... }
Step 4: Create an implementation of the View, inject the Presenter using @Inject, and now supports Fragmnt, Activit, View
public class MyView extends . implements IMyView { @Inject MyPresenter mPresenter; @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { (view, savedInstanceState); (this); } @Override void show(String something) { /* ... */ } // ... }
Step 5: The Activity contains the Fragment and View of Presenter, so the Activity needs to be explained using @Blade to let Blade know.
State
To simplify state management, the @State annotation will generate a help class containing two static methods:
public class StateArgFragment extends Fragment { @Arg @State int num; } @Weave( into = "0_onSaveInstanceState", args = {""}, statement = "eu..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 = (state); ("<Stateful-num>", ); } @Weave( into = "1^onCreate", args = {""}, statement = "eu..StateArgFragment_Helper.restoreState(this, $1);" ) public static void restoreState(StateArgFragment target, Bundle state) { if (state == null) { return; } BundleWrapper bundleWrapper = (state); = ("<Stateful-num>", ); }
Classes inherited from Fragment, Activity, or View will automatically manage state. The functionality of custom serialization is shown above.
Blade allows us to write a lot of boilerplate code. I haven't applied the specific code to the project yet, and will be used in the project later to make the project look clearer.
Blade Address:/FrantisekGazo/Blade
Summarize
The above is the use of Blade in Android introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!