Android naming conventions for Emergency keys for Advanced and Binding keys options

I am currently learning how to program on Android. I read that the keys for Extras (to be in intention) usually begin with the word "EXTRA", for example:

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

And that keys to objects that should be stored in the Bundle usually begin 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 transfer to another activity as Extra, but I also need to save the same variable in the Bundle for some activity? Should I

  • have two keys for the variable (i.e. have both EXTRA_USER_CHEATED and KEY_USER_CHEATED) or
  • Is there one key for the variable (this idea seems better to me, but I'm a complete 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)?
+4
source share
1 answer

I cannot be sure of the answer, but in my opinion EXTRA_MESSAGE OR KEY is just the key to some value. You can have 2 different keys that point to the same data, therefore, to answer your question, maybe just both of them (that is, option 1).

This short piece of code can give you a key ... note that the String message is the value associated with the key, EXTRA_MESSAGE (. putExtra).

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);
}
+3

Source: https://habr.com/ru/post/1526580/


All Articles