-
Notifications
You must be signed in to change notification settings - Fork 25
A companion object
Madhu kiran edited this page Jan 17, 2016
·
1 revision
If having the fields at a global scope is a problem an inner class can be used to instead. For example, consider the BookDetailActivity
:
@RequireBundler
public class BookDetailActivity extends Activity {
@Arg
long id;
@Arg
String book;
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
Bundler.inject(this);
}
}
Instead of that you can use something like this:
public class BookDetailActivity extends Activity {
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
Companion companion = new Companion(getIntent());
// companion is injected with intent extras
}
// convenience method to start the activity
public static void start(long id, String book, Context ctx) {
Intent intent = new Intent(ctx, BookDetailActivity.this)
.putExtras(Bundler.bookDetailActivity(id, book).bundle());
ctx.startActivity(intent);
}
@RequireBundler(bundlerMethod="bookDetailActivity")
static class Companion {
@Arg
long id;
@Arg
String book;
Companion(Intent intent) {
if(intent != null) {
Bundler.inject(this, intent.getExtras());
}
}
}
}
Now this activity can be started using BookDetailActivity.start(id, book, ctx)
. This can be a useful pattern to follow if limiting the scope of variables is important. The additional code overhead for setting this up is a pain, this can be eliminated by modifying the library, but I'm not sure how important limiting the scope is. If I'm underestimating the importance of this please create an issue for support of this feature.