i don't understand that this training code for android studio - android

https://developer.android.com/training/basics/firstapp/starting-activity
public class MainActivity extends AppCompatActivity {
public static final String SIGNUP_EMAIL = "com.example.myapplication.SIGNUP_EMAIL";
public static final String SIGNUP_PASSWORD = "com.example.myapplication.SIGNUP_PASSWORD";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void signup(View view) {
Intent intent = new Intent(this, SignupResultActivity.class);
EditText signup_email = (EditText) findViewById(R.id.signup_email);
EditText signup_password = (EditText) findViewById(R.id.signup_password);
String email = signup_email.getText().toString();
String password = signup_password.getText().toString();
intent.putExtra(SIGNUP_EMAIL, email);
intent.putExtra(SIGNUP_PASSWORD, password);
startActivity(intent);
}
}
public static final String SIGNUP_EMAIL = null;
public static final String SIGNUP_PASSWORD = null;
why should not use "null" in this code?
if you put SIGNUP_EMAIL=null and SIGNUP_PASSWORD=null, is not working

Intent is similar to Map, the value of these variables are the keys to index this map. Not only those keys must be non-null, but they must be different from each other.
Quoted from Start another activity (emphasis mine):
The putExtra() method adds the EditText's value to the intent. An Intent can carry data types as key-value pairs called extras. Your key is a public constant EXTRA_MESSAGE because the next activity uses the key to retrieve the text value. It's a good practice to define keys for intent extras using your app's package name as a prefix. This ensures the keys are unique, in case your app interacts with other apps.

SIGNUP_EMAIL and SIGNUP_PASSWORD are not default values for mail and password, they are string keys used to pass ang get the values, so they cannot be null.
They are public constants so that the intent can know them to retreive the value.
EDIT: note that the strings are declared final so that the keys cannot be changed afterwards, having a property declared final with a null value should have raised a mental flag as it is a bit useless..

when you want to pass a data from one activity to another one you should use intent.putExtra(name,data);
first parameter is the name and second is data that you want to send
you can use any string that you want for name parameter but it cant be null
in second activity you use it to get data from first activity by this code :
intent.getStringExtra(name)

You can use any unique string to map the values. Null wont work but "null" will work. But keep one thing in mind that for two different values you need to assign different keys also.
Like this:
public static final String SIGNUP_EMAIL = "null";
public static final String SIGNUP_PASSWORD = "Null";

Related

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
have two keys for the variable (i.e. have both EXTRA_USER_CHEATED and KEY_USER_CHEATED), or
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)?
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);
}

How is the information passed here using intents

From my Sudoku.java I make call to Game.java and pass the difficulty level along with.
private void startGame(int i) {
Log.d(TAG, "clicked on " + i);
Intent intent = new Intent(Sudoku.this, Game.class);
intent.putExtra(Game.KEY_DIFFICULTY, i);
startActivity(intent);
}
Here's part of my Game.java
public class Game extends Activity {
private static final String TAG = "Sudoku" ;
public static final String KEY_DIFFICULTY = "org.example.sudoku.difficulty" ;//What is this?
public static final int DIFFICULTY_EASY = 0;
public static final int DIFFICULTY_MEDIUM = 1;
public static final int DIFFICULTY_HARD = 2;
private int puzzle[] = new int[9 * 9];
private PuzzleView puzzleView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate" );
int diff = getIntent().getIntExtra(KEY_DIFFICULTY,DIFFICULTY_EASY); //What is this?
puzzle = getPuzzle(diff);
calculateUsedTiles();
puzzleView = new PuzzleView(this);
setContentView(puzzleView);
puzzleView.requestFocus();
}
}
My question is what is happening in line
public static final String KEY_DIFFICULTY = "org.example.sudoku.difficulty" ;
KEY_DIFFICULTY is final, how will i ever change it.
Also, while fetching the extra data info from intent, how is it storing it?
the value of this public static final String KEY_DIFFICULTY = "org.example.sudoku.difficulty" ; is not changing but imagine intent has a bucket(a hashmap) and KEY_DIFFICULTY is a key in it
You are storing a value against that key and getting it back via intent. You never change the key it self as it is final.
Even if you remove final it will work but you must retrieve the value with the same key you set it with.
So KEY_DIFFICULTY never changes but the value against it in extras bundle is set and retrieved.
public static final String KEY_DIFFICULTY =
"org.example.sudoku.difficulty" ;//What is this?
=> First of all, Intent stores data in a key-value pairs, same like HashMap. So whatever data you would want to store/fetch into/from it, you have to have KEY name.
Now as you have already made KEY_DIFFICULTY as static final, you won't be able to change it as it becomes constant.
Key is used to get the extra data so it should not be changed. And an intent can contain or storing datas via a Bundle. And for more clarification please look at Android Intents.
Cheers

How to clear values of all static variables at the end of an activity Android?

I have created an application that takes in user name and other details for a transaction and then fills them in a database. At times the application shows odd behavior by filling the SAME details in the database twice as two transactions. Even though the new values are read but not STORED in the static variables.
Therefore I needed help in flushing the values of all my static variables at the end of each activity to avoid overriding of the previous values in a fresh transaction.
EDIT :
public class One
{
static String var;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
var="blah";
}
}
public class Two
{
static String variable = One.var;
// This is where i am accessing the value of the variables from previous activities.
//CODE
}
May these help you..
Using Static Variables is like a nightmare in any activity as it stores memory through out the activity..
I think you can try some other memory store to overcome your problem of passing value from one activity to another..
In my opinion u can store values in SharedPreference or either you can pass value through intent to other activity where ever it is required..
Hope these will help you..
EDIT:
Intent in = new Intent(MainActivity.this,SecondActivity.class);
You can use more than one putExtra() method to put several values and can fetch then in Second Activity
in.putStringArrayListExtra(String name, ArrayList<String> value);
StartActivity(in);
In Second Activity:
Intent in = getIntent();
ArrayList<String> Roleids = new ArrayList<String>;
RoleId = in.getStringArrayListExtra(String name, ArrayList<String> value)

Problems using an EditText

I'm trying to pass the value of an editText from a class to another. In the first class, I use this code to obtain the value of the editText:
number = (EditText) this.findViewById(R.id.editText10);
text=number.getText().toString();//obtain the value
where "text" is a static string. Later I use this code that returns the STATIC string "text":
public static String rete()
{
return text;
}
And finally, I get the value in the second class using this:
String text2 = Pruebita2.rete();
where Pruebita2 is the name of the first class.
What am i doing wrong?
Easiest way to transfer data between classes is to pass the string "text" via an intent to the second class.
Eg.
Activity1: Create the intent
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("text_key", text);
context.startActivity(intent);
Activity2: Get the vlaue
text = getIntent().getStringExtra("text_key");

Android Split and TextUtils.split not working when assigning to private array

okay so in my other android activity I have it display a button on the screen if a certain string is in the array. I will be using the split function on a stored string to turn my string back into an array and then assinging that array into my private favorites array. In this example I am trying to get it to display a button if the string "UltimateBP" is in the private array favorites.
if i assign it directly using:
favorites[1]="UltimateBP";
it works and the button shows up correctly.
however if I assign it using the method below. it will not show up.
It does the same thing when I use the TextUtils split() method.
public class SampleApplication extends Application{
private String mStringValue;
private int numOfFavorites=1;
private String[] favorites = new String[150];
#Override
public void onCreate() {
mStringValue = "SavageLook.com";
favorites[0] = "None";
String someWords = "UltimateBP|Orange|Yellow";
String aColors[] = someWords.split("\\|");
numOfFavorites++;
String X = aColors[0];
favorites[1]=X;
super.onCreate();
}
Instead of using:
String aColors[] = someWords.split("\\|");
You just need to show "|" like:
String aColors[] = someWords.split("|");

Categories

Resources