1

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.

erik
  • 4,946
  • 13
  • 70
  • 120
  • 1
    Who will be using this activity? Just you? Members of your development team? People outside of your immediate team (including the general public, if this will be for a library to be distributed)? – CommonsWare Apr 19 '17 at 19:22
  • members of my development team – erik Apr 19 '17 at 19:29
  • i should add that that extra will be a public static enum of the activity being called.. for arguments sake, lets call that enum Mode and say it has two values: A and B – erik Apr 19 '17 at 19:30

1 Answers1

2

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • are you suggesting that my activity should have something like this: public static Intent IntentFactory(Context context, Mode mode){ Intent intent = new Intent(); intent.setClass(context,MyNewActivity.class); intent.putExtra(Mode.class.getName(),mode.name()); return intent; } – erik Apr 19 '17 at 19:38
  • @erik: Sorry, but your comment was eaten by a grue. I'm suggesting something like `YourActivity.start()`, where `YourActivity` is your activity, and `start()` takes a `Context` (for starting the activity), your `enum`, and anything else that could be packaged as extras. For a really complicated set of extras, you could create a full `IntentBuilder` -- I took that route with my CWAC-Cam2 library, to make it easy to assemble a suitable `Intent`. – CommonsWare Apr 19 '17 at 19:42
  • Can you mock that up for me real quick to insure im understanding, with that helper in myactivity and an example of "theirActivity" calling it? – erik Apr 19 '17 at 19:45