A solution to make it easy to launch an activity with parameters.
- An activity launcher can be define by the
@Launcher
annotation, and the@Required
annotation means the parameter has to be delivered when launching this activity. On the contrary, the@Optiinal
annotation means the parameter can be delivered or not:
@Launcher(
flags = {Intent.FLAG_ACTIVITY_CLEAR_TOP},
pendingTransition = R.anim.anim_slide_right_in,
pendingTransitionOnFinish = R.anim.anim_slide_left_out
)
public class UserInfoActivity extends AppCompatActivity {
@Required int userId;
@Optional boolean isEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ActivityLauncher.fill(this, savedInstanceState);
}
}
- And that is how you launch the activity:
UserInfoActivityLauncher.builder(userId)
.isEdit(true)
.start(context);
- Also, you can use the
@OnActivityResult
annotation to handle the activity result calling:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
UserInfoActivityLauncher.builder(userId)
.isEdit(true)
.startForResult(context, requestCode);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ActivityLauncher.dispatchResult(this, requestCode, resultCode, data);
}
@OnActivityResult(requestCode = 1, resultCode = RESULT_OK)
public void handleTakePhoto(Intent data) {
// TODO: do something.
}
}
1 Add the repository address in builde.gradle(Project)
file:
allprojects {
repositories {
maven {
url uri('https://raw.githubusercontent.com/cooperise/ActivityLauncher/master/repo')
}
}
}
2 To compile file in builde.gradle(Module)
file:
implementation 'cn.cooper.support:activitylauncher:1.0.0'
annotationProcessor 'cn.cooper.support:activitylauncher-compiler:1.0.0'