I am creating an activity that I wish to require a specific extra in the intent bundle.
I am wondering what the best practice would be for such. The plan is to not start, or immediately finish the activity if the bundle does not include a specific extra. I'd also like to notify the caller with an error stating such.
The plan is to not start, or immediately finish the activity if the bundle does not include a specific extra.
Validate the extra in onCreate()
, and call finish()
if the extra is missing/invalid.
I'd also like to notify the caller with an error stating such.
You can log something to LogCat. However, you have no means of throwing an exception to the code that called startActivity()
.
You might consider creating some form of static
helper method on the activity that either:
just assembles the Intent
in the way that you want it, or
also goes ahead and calls startActivity()
, given a suitable Context
and encourage your development team to use that method (bribe them with golf clubs, threaten them with golf clubs, etc.). For example, on MyActivity
, have:
public static void start(Context ctxt, YourEnum value) {
Intent i=new Intent(ctxt, MyActivity.this).putExtra(EXTRA_THINGY, value);
ctxt.startActivity(i);
}
and your team members can call MyActivity.start(this, MyEnum.AWESOME);
to start your activity. You cannot force them to do that programmatically, though (e.g., you cannot force a compile-time error if they create the Intent
on their own).
note that golf clubs is an example, not a best practice — for example, depending on your baking skills, you can bribe/threaten them with cookies