Put 2 extras in intent - java

I've made an intent and i've put inside 2 extras
Intent intent = new Intent(MainActivity.this, Options.class);
TextView labelName = (TextView) findViewById(R.id.label1); // value = "Counter1"
TextView label2Name = (TextView) findViewById(R.id.label2); // value = "Counter 2"
String lblNameDefault = labelName.getText().toString();
String lbl2NameDefault = label2Name.getText().toString();
intent.putExtra(LABEL_NAME_DEFAULT, lblNameDefault);
intent.putExtra(LABEL_2_NAME_DEFAULT, lbl2NameDefault);
In my other activity i retrieve the info from them like this
//Get name from the label
Intent intent = getIntent();
String lblNameDefault = intent.getStringExtra(MainActivity.LABEL_NAME_DEFAULT);
String lbl2NameDefault = intent.getStringExtra(MainActivity.LABEL_2_NAME_DEFAULT);
//Set current name to editText
EditText labelNameDefault = (EditText)findViewById(R.id.set_name);
EditText label2NameDefault = (EditText)findViewById(R.id.set_name2);
labelNameDefault.setText(lblNameDefault, TextView.BufferType.EDITABLE);
label2NameDefault.setText(lbl2NameDefault, TextView.BufferType.EDITABLE);
The problem is that i receive to both labelNameDefault and label2NameDefault the result from LABEL_2_NAME_DEFAULT.
Can i only pass one extra?
How can i pass them both?
By default the value of labelName is "Counter 1" and the value of label2Name is "counter 2"
If i comment out intent.putExtra(LABEL_2_NAME_DEFAULT, lbl2NameDefault); the first label name is ok.
It looks like LABEL_2_NAME_DEFAULT is overwriting LABEL_NAME_DEFAULT

You could pass a 'bundle' of extras rather than individual extras if you like, for example:-
Intent intent = new Intent(this, MyActivity.class);
Bundle extras = new Bundle();
extras.putString("LABEL_NAME_DEFAULT",lblNameDefault);
extras.putString("LABEL_2_NAME_DEFAULT",lbl2NameDefault);
intent.putExtras(extras);
startActivity(intent);
Then in your Activity that your triggering, you can reference these like so:-
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String username_string = extras.getString("LABEL_NAME_DEFAULT");
String password_string = extras.getString("LABEL_2_NAME_DEFAULT");
Or (if you prefer):-
Bundle extras = getIntent().getExtras();
String username_string = extras.getString("LABEL_NAME_DEFAULT");
String password_string = extras.getString("LABEL_2_NAME_DEFAULT");
Hope this helps! :-)

You can pass two (or more) extras in an intent. You need to make sure that LABEL_2_NAME_DEFAULT and LABEL_NAME_DEFAULT does not have the same value, though.

activity one
Intent intent = new Intent(MainActivity.this, Options.class);
TextView labelName = (TextView) findViewById(R.id.label1); // value = "Counter1"
TextView label2Name = (TextView) findViewById(R.id.label2); // value = "Counter 2"
String lblNameDefault = labelName.getText().toString();
String lbl2NameDefault = label2Name.getText().toString();
intent.putExtra("LABEL_NAME_DEFAULT", lblNameDefault);
intent.putExtra("LABEL_2_NAME_DEFAULT", lbl2NameDefault);
activity two
username_string = getIntent().getExtras().getString("LABEL_NAME_DEFAULT");
password_string = getIntent().getExtras().getString("LABEL_2_NAME_DEFAULT");

Related

Can't pass string value

I want to create a simple calulator, and I get value of second activity of 0 no matter what I write in text View.
Intent intent = new Intent(this, DisplayResultActivit.class );
EditText edittext = findViewById(R.id.liczba);
EditText edittext2 = findViewById(R.id.liczba2);
int wpis2 = Integer.valueOf(edittext.getText().toString());
int wpis = Integer.valueOf(edittext2.getText().toString());
Bundle extras = new Bundle();
extras.putInt("wpis", wpis);
extras.putInt("wpis2", wpis2);
startActivity(intent);
2 activity :
Intent intent = getIntent();
Bundle extras = intent.getExtras();
int wpis = 0;
if (extras != null) {
wpis = extras.getInt("wpis1");
}
int wpis2 = 0;
if (extras != null) {
wpis2 = extras.getInt("wpis2");
}
TextView tv = findViewById(R.id.result);
tv.setText(String.valueOf(wpis) + String.valueOf(wpis2));
As mention in the comment. You also need to link the Bundle variable with the Intent variable. See the follow link: https://zocada.com/using-intents-extras-pass-data-activities-android-beginners-guide/
//create a Bundle object
Bundle extras = new Bundle();
//Adding key value pairs to this bundle
//there are quite a lot data types you can store in a bundle
extras.putString("USER_NAME","jhon Doe");
extras.putInt("USER_ID", 21);
extras.putIntArray("USER_SELCTIONS", [1, 2, 3, 4, 5]);
...
//create and initialize an intent
Intent intent = new Intent(this, NextActivity.class);
//attach the bundle to the Intent object
intent.putExtras(extras);
//finally start the activity
startActivity(intent);
So your code needs to be:
Intent intent = new Intent(this, DisplayResultActivit.class );
EditText edittext = findViewById(R.id.liczba);
EditText edittext2 = findViewById(R.id.liczba2);
int wpis2 = Integer.valueOf(edittext.getText().toString());
int wpis = Integer.valueOf(edittext2.getText().toString());
Bundle extras = new Bundle();
extras.putInt("wpis", wpis);
extras.putInt("wpis2", wpis2);
intent.putExtras(extras);
startActivity(intent);
Just change your code to:
Intent intent = new Intent(this, DisplayResultActivit.class );
EditText edittext = findViewById(R.id.liczba);
EditText edittext2 = findViewById(R.id.liczba2);
int wpis2 = Integer.valueOf(edittext.getText().toString());
int wpis = Integer.valueOf(edittext2.getText().toString());
Bundle extras = new Bundle();
extras.putInt("wpis", wpis);
extras.putInt("wpis2", wpis2);
intent.putExtras(extras);
startActivity(intent);
You forgot about putExtras() method!

GetStringExtra always return null

I tried to send string from onclick recyclerview to the activity, all doing well except one of this.
GeneralItem generalItem = (GeneralItem) consolidatedList.get(position);
Intent intent = new Intent(getActivity(), DetailPengumuman.class);
intent.putExtra("getnama", generalItem.getDaftarPengumuman().getNama_p().toString());
Log.e("untaging","ada isinya : "+generalItem.getDaftarPengumuman().getNama_p().toString());
intent.putExtra("tanggalpengumuman", generalItem.getDaftarPengumuman().getTanggal_peng());
intent.putExtra("judulpengumuman", generalItem.getDaftarPengumuman().getJudul());
intent.putExtra("deskripsipengumuman", generalItem.getDaftarPengumuman().getDeskripsi());
startActivity(intent);
I also tried to log getnama in untaging tag its doing well and return me the data in log. But when I retrieve it in another activity, It always return null.
Intent intent = getIntent();
tanggalPengumumanGet = intent.getStringExtra("tanggalpengumuman");
judulPengumumanGet = intent.getStringExtra("judulpengumuman");
namaPengumumanGet = intent.getStringExtra("getnama");
deskripsiPengumumanGet = intent.getStringExtra("deskripsipengumuman");
Log.e("untaging","nama : " +namaMatkulGet);
You can first check for if intent contains data or not..
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if (bundle.containsKey("Key")) {
String value = bundle.getString("Key");
}
}
Trying Adding .toString() to call getStringExtra :
intent.putExtra("tanggalpengumuman", generalItem.getDaftarPengumuman().getTanggal_peng().toString());
intent.putExtra("judulpengumuman", generalItem.getDaftarPengumuman().getJudul().toString());
intent.putExtra("deskripsipengumuman", generalItem.getDaftarPengumuman().getDeskripsi().toString);
Solved thanks,
This because of I missing the String attribute
Intent intent = getIntent();
tanggalPengumumanGet = intent.getStringExtra("tanggalpengumuman");
judulPengumumanGet = intent.getStringExtra("judulpengumuman");
namaPengumumanGet = intent.getStringExtra("getnama");
deskripsiPengumumanGet = intent.getStringExtra("deskripsipengumuman");
Log.e("untaging","nama : " +namaMatkulGet);
I receiving extra in namaPengumumanGet and I log another String which is namaMatkkulGet

How to add/pass multiple values to Intent object?

i have two EditText objects in my first activity. i want both their values when i go to the next activity.
Lets assume the EditText objects are inp1, inp2 and they can only accept numbers.
please mention how i can add their values to int Intent object and how i will extract their values in my next activity's .java file.
Here we go, your code will look like,
Sender Side:
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName1", intValue1);
myIntent.putExtra("intVariableName2", intValue2);
startActivity(myIntent);
Receiver Side:
Intent mIntent = getIntent();
int intValue1 = mIntent.getIntExtra("intVariableName1", 0);
int intValue2 = mIntent.getIntExtra("intVariableName2", 0);
Hope it helps.
Use this code
Intent intent = new Intent(first.this, Second.class);
Bundle extras = new Bundle();
extras.putString("value1",String.valueof(inp1.getText().toString()));
extras.putString("value2",String.valueof(inp2.getText().toString()));
intent.putExtras(extras);
startActivity(intent);
Then in your second Activity onCreate()
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String value1 = extras.getString("value1");
String value2 = extras.getString("value2");
To make thing easier and reusable you can make your own intent like this
public class MyIntent extent Intent{
private static final String FIRST_VALUE;
private static final String SECOND_VALUE;
public MyIntent(Context context, String firstValue, String secondValue){
super(context,MySecondActivity.class);
putExtra(FIRST_VALUE, firstValue);
putExtra(SECOND_VALUE, secondValue);
}
public String getFirstValue(){
getStringExtra(FIRST_VALUE);
}
public String getSecondValue(){
getStringExtra(SECOND_VALUE);
}
}
Sender:
startActivity(new MyIntent(this,"FirstString", "SecondString"));
Receiver Side:
MyIntent myIntent = (MyIntent)getIntent();
String firstValue = myIntent.getFirstValue();
String secondValue = myIntent.getSecondValue();

How to pass integer to a new activity in Android?

I'm new to android. I am taking a string and a numerical value as input from the MainActivity.
On pressing the button, the following method is called which invokes the second activity.
Am I passing the values correctly?
If yes, how do I receive both these values for use in the second activity and then print them ?
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText1 = (EditText) findViewById(R.id.name);
EditText editText2 = (EditText) findViewById(R.id.ma);
String message = "Hi ! " + editText1.getText().toString();
int i = Integer.parseInt(editText2.getText().toString());
intent.putExtra("lol",message);
startActivity(intent);
}
P.S. I know Im not passing the integer at all. I dont know how to do that. Please help!
From the new Activity you should call getIntent(): https://developer.android.com/reference/android/app/Activity.html#getIntent()
Once you have the Intent you have to call getIntExtra(String, int): https://developer.android.com/reference/android/content/Intent.html#getIntExtra(java.lang.String,%20int).
In your case, from the new Activity something like:
Intent intent = getIntent();
int lol = intent.getIntExtra("lol", 0);
Or shorter:
int lol = getIntent().getIntExtra("lol", 0);
That '0' is the default value for the int if the extra "lol" does not exist.
You just use this one for integer values
In second activity....
int i = getIntent().getIntExtra(SendingStringName, 0);
(or)
String progress = getIntent().getStringExtra(SendingStringName);
(or)
ArrayList< String> progress = getIntent().getStringArrayListExtra(name);
To send the data:
EditText editText1 = (EditText) findViewById(R.id.name);
EditText editText2 = (EditText) findViewById(R.id.ma);
String message = "Hi ! " + editText1.getText().toString();
int i = Integer.parseInt(editText2.getText().toString());
Bundle param = new Bundle();
param.putString("greeting",message);
param.putInteger("NumberInteger",i);
Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.putExtras(param);
startActivity(intent);
To receive them:
In Class DisplayMessageActivity.java do:
Intent it = getIntent();
Bundle param = it.getExtras();
String capturedMessage= param.getString("greeting");
Integer captured_I = param.getInteger("NumberInteger");

Data passing via intent

I am trying to pass a selected date from CalendarView via an intent from activity no1 to activity no2. The activity no2 opens without any problems but the selected date is not displayed.
Here is the code for activity no1:
String syear = Integer.toString(year);
String smonth = Integer.toString(month);
String sday= Integer.toString(day);
Intent myIntent = new Intent(CalendarActivity.this, CalendarDate.class);
Bundle extras = new Bundle();
myIntent.putExtra(syear, "currentyear");
myIntent.putExtra(smonth,"currentmonth");
myIntent.putExtra(sday,"currentday");
myIntent.putExtras(extras);
CalendarActivity.this.startActivity(myIntent);
and this is the code for activity no2:
Bundle extras = getIntent().getExtras();
String dyear = extras.getString("currentyear");
String dmonth = extras.getString("currentmonth");
String dday = extras.getString("currentday");
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(dday);
textView.setText(dmonth);
textView.setText(dyear);
setContentView(textView);
What am I doing wrong ?
It should be
myIntent.putExtra("currentyear",syear);
myIntent.putExtra("currentmonth",smonth);
myIntent.putExtra("currentday",sday);
Also use append
textView.append(dday);
textView.append(dmonth);
textView.append(dyear);
In activity no 1 have like this.
myIntent.putExtra("currentyear", syear);
myIntent.putExtra("currentmonth",smonth);
myIntent.putExtra("currentday",sday);
myIntent.putExtras(extras);
And in the activty no 2
Always have the null check placed when reading from the extras.
have like this.
Bundle extras = getIntent().getExtras();
if(extras!=null){
String dyear = extras.getString("currentyear");
String dmonth = extras.getString("currentmonth");
String dday = extras.getString("currentday");
}
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(dday);
textView.setText(dmonth);
textView.setText(dyear);
setContentView(textView);

Categories

Resources