Reputation: 93
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
Upvotes: 6
Views: 3555
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