user3229293
user3229293

Reputation: 93

Android naming conventions for Intent "Extra" keys vs. Bundle keys

I am currently learning how to program in Android. I read that the keys for Extras (to be put in an intent) usually start with the word "EXTRA", for example:

public static final String EXTRA_USER_CHEATED = "some unique string";

And that keys to objects that are to be saved in the Bundle usually start with the word "KEY", for example:

public static final String KEY_USER_CHEATED = "some other unique string";

What if I have a variable that I need to pass to another activity as an Extra, but I also need to be able to save that same variable in the Bundle for an activity? Should I

  1. have two keys for the variable (i.e. have both EXTRA_USER_CHEATED and KEY_USER_CHEATED), or
  2. have a single key for the variable (this idea seems better to me, but I am a total Android newbie)? If so, what should it be called (should it be called EXTRA_USER_CHEATED, KEY_USER_CHEATED, just USER_CHEATED, or something else)?

Upvotes: 6

Views: 3555

Answers (1)

Kevin Lee
Kevin Lee

Reputation: 2347

I can't be sure of the answer, but from my understanding, the EXTRA_MESSAGE OR the KEY is merely a key to some value. You can have 2 different keys which point to the same data, so to answer your question, maybe just have both (i.e. option 1).

This short code snippet might give you a clue... notice that String message is the value associated with the key which is EXTRA_MESSAGE (see documentation for the putExtra method).

public static final String EXTRA_MESSAGE = "com.whatever.appName.MESSAGE";

public void sendMessage(View view){
    Intent intent = new Intent(this, DisplayMessageActivity.class); 

    EditText editText = (EditText) findViewById(R.id.edit_message);    

    String message = editText.getText().toString(); 

    intent.putExtra(EXTRA_MESSAGE, message);

    startActivity(intent);
}

Upvotes: 4

Related Questions