3

Any ideas in the best way to send an IntDef through an Intent?

ps: If you simply send it as an int you loose the type check, which is the main goal of having the intdef

lage
  • 589
  • 2
  • 5
  • 18
  • basically, use it as an int and convert to your IntDef via a helper method. helpful reading, http://stackoverflow.com/a/32160715/794088 & http://stackoverflow.com/a/31737425/794088 – petey Oct 27 '15 at 16:41

1 Answers1

0

Your IntDef is not available at runtime. The way to go is to encapsulate the generation of the intent in your intent-receiving Activity.

public class MyActivity extends Activity {

    private static final String KEY_NAVIGATION_MODE = "navigation_mode";
    private @NavigationMode int mNavigationMode;

    public static Intent getStartIntent(Context context, @NavigationMode String navigationMode) {
        Intent intent = new Intent(context, MyActivity.class);
        intent.putExtra(KEY_NAVIGATION_MODE, navigationMode);
        return intent;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //noinspection WrongConstant Safe because we saved it in #getStartIntent
        mNavigationMode = getIntent().getIntExtra(KEY_NAVIGATION_MODE, NAVIGATION_MODE_YOUR_DEFAULT);
    }
}
sorianiv
  • 4,845
  • 1
  • 23
  • 27