Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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)?
like image 665
user3229293 Avatar asked Feb 12 '14 16:02

user3229293


People also ask

What is the naming convention for Android?

It's the end of an era at Google: The company's Android mobile platform is dropping its long-running dessert-themed naming convention, switching instead to a simple numerical format. Beginning with the release of Android 1.5, the mobile popular platform Google acquired along with its creator Android Inc.

What is put extra in intent?

Using putExtra() and getExtras() in android Intents are asynchronous messages which allow Android components to request functionality from other components of the Android system. For example an Activity can send an Intents to the Android system which starts another Activity .


1 Answers

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);
}
like image 196
Kevin Lee Avatar answered Nov 15 '22 01:11

Kevin Lee