the intent always return the default value - android

i have many activities and i need to move an integer data from one to another one >>
in the first activity i have this code
final Intent go_to_main = new Intent(getBaseContext() , MainActivity.class);
go_to_main.putExtra("data" , i1) ;
and the secound one
Intent intent = getIntent() ;
data = intent.getIntExtra("data",3) ;
bbut always return the default which is 3 here
i have tried the bundle but when i use it like that my app stoping when i click the button to move to the secound activity
Intent go_to_main = new Intent(getApplicationContext() , MainActivity.class);
Bundle mBundle = new Bundle();
mBundle.putInt("data", i1);
go_to_main.putExtras(mBundle);
and in the secound one i have this code
Intent intent = getIntent() ;
data = intent.getExtras().getInt("data") ;
the error in the logcat is :
2020-05-30 18:27:56.461 14976-15008/com.e.pluspath E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1

Don't think you have to use bundle.
for go_to_main intent:
Intent go_to_main = new Intent(getApplicationContext() , MainActivity.class);
go_to_main.putExtra("data", i1);
And for your receiving end:
Intent intent = getIntent() ;
data = intent.getIntExtra("data", 0);

Related

How i can send data variable to multiple activity at the same time?

String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
Intent intent1 = new Intent(DeviceList.this, MainActivity.class);
intent1.putExtra(EXTRA_ADDRESS, address);
startActivity(intent1);
Intent intent12 = new Intent(DeviceList.this, Room1.class);
i.putExtra(EXTRA_ADDRESS, address);
how i can send the same data at the same time using intent to multiple activity without start this activity ?

Sending multiple integers with intent

i am trying to send two values to another page, but everytime only one of them is working.Here is my code:
Sending:
Intent i = new Intent(getApplicationContext(),fight_1.class);
i.putExtra("deger2",ana_ekran_arti1_int);
startActivity(i);
Intent i = new Intent(getApplicationContext(),fight_1.class);
i.putExtra("deger", ana_ekran_can_int);
startActivity(i);
Taking:
int i = getIntent().getIntExtra("deger",-1);
int ii = getIntent().getIntExtra("deger2",-1);
arti1= ii;
fight_1_arti1.setText(arti1+"");
fight_1_can_int=i;
fight_1_can.setText(fight_1_can_int+"");
You are only sending one value you have to use the same intent without creating a new object.
I hope this helps
Intent i = new Intent(getApplicationContext(),fight_1.class);
i.putExtra("deger2", ana_ekran_arti1_int);
i.putExtra("deger", ana_ekran_can_int);
startActivity(i);

Android programming passing values

Sorry if we're doing anything wrong, we just started a crash course in android and we are trying to pass a value using the bundle but it does not show any error, it only crashes after the attempt. Here is the code block below.
ps, pls feel free to edit the post if you see anything wrong with it.
Bundle b = new Bundle(); //to pass values in meals.java
b.putStringArray("breakfast1" , new String[]{breakfast1[0]});
Intent gene = new Intent(Calories.this, Days.class);
//to pass the activity in another activity
gene.putExtras(gene);
startActivity(gene);
//second.java bundle receiving code block
break1 = (TextView) findViewById(R.id.break1);
Intent gene = getIntent();
String[] break2 = gene.getStringArrayExtra("breakfast1");
break1.setText(break2[0]);
Change gene.putExtras(gene); to gene.putExtras(b); and retrieve bundle using b = getIntent().getExtras()
FirstActivity
Bundle b = new Bundle(); //to pass values in meals.java
b.putStringArray("breakfast1" , new String[]{breakfast1[0]});
Intent gene = new Intent(Calories.this, Days.class);
//to pass the activity in another activity
gene.putExtras(b); //put bundle to intent
startActivity(gene);
In Second Activity Second.java
break1 = (TextView) findViewById(R.id.break1);
Intent gene = getIntent();
Bundle b = gene.getExtras(); //get bundle from intent
String[] break2 = b.getStringArrayExtra("breakfast1"); //get array list from bundle
break1.setText(break2[0]);
replace with this.
Bundle b = new Bundle(); //to pass values in meals.java
b.putStringArray("breakfast1" , new String[]{breakfast1[0]});
Intent gene = new Intent(Calories.this, Days.class);
//to pass the activity in another activity
gene.putExtras(b); // put b not gene
startActivity(gene);
//second.java bundle receiving code block
break1 = (TextView) findViewById(R.id.break1);
Intent gene = getIntent();
String[] break2 = gene.getStringArrayExtra("breakfast1");
break1.setText(break2[0]);
Please try this
Bundle b = new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i = new Intent(context, Class);
i.putExtras(b);
And for receiving side
Bundle b = this.getIntent().getExtras();
String[] array = b.getStringArray(key);

Two EXTRA_MESSAGE from Intent

Can i have two extra messages on Intent that i can pass on another Activity? If yes, how does it work?
this is what I did:
Intent intent3 = new Intent(this, Start.class);
String message = o.getName();
String messages = o.getPath();
intent3.putExtra(EXTRA_MESSAGE, message);
intent3.putExtra(EXTRA_MESSAGE, messages);
startActivity(intent3);`
and on the Activity that will receive the message:
Intent intent3 = getIntent();
String message = intent3.getStringExtra(Browse.EXTRA_MESSAGE);
String messages = intent3.getStringExtra(Browse.EXTRA_MESSAGE);`
You have to change the KEY:
Intent intent3 = new Intent(this, Start.class);
String message = o.getName();
String messages = o.getPath();
intent3.putExtra(EXTRA_MESSAGE, message);
intent3.putExtra(EXTRA_MESSAGE_TWO, messages);
startActivity(intent3);
Not exactly like that, but it is easy to send multiple values. The first parameter to putExtra or getStringExtra is a key into a map of values so it must be unique. As long as you use a different key for each value you can put as many as you want in the intent.
For example:
Intent intent3 = new Intent(this, Start.class);
String message = o.getName();
String messages = o.getPath();
intent3.putExtra("name", message);
intent3.putExtra("path", messages);
startActivity(intent3);
Intent intent3 = getIntent();
String message = intent3.getStringExtra("name");
String messages = intent3.getStringExtra("path");`
This is a problem the way you have them. You need separate keys for each value. So for example
intent3.putExtra("msg1", message);
intent3.putExtra("msg2", messages);
then in your receiving Activity you can get them using the above keys. But you can add as many Extra values as you want.
Two different keys and messages will do it
Intent intent3 = new Intent(this, Start.class);
String message = o.getName();
String messages = o.getPath();
intent3.putExtra(EXTRA_MESSAGE1, message);
intent3.putExtra(EXTRA_MESSAGE2, messages);
startActivity(intent3);

intent.putExtra not working

I need to pass information between two activities, but for some reason the information isn't sent / recieved.
LogCat doesn't give me any errors. The dubugger clearly shows something is added to the intent (variabl: mExtras), but it's hard to interpret exactly what is added. After that it gives me "source not found" and doesn't help me further.
But first things first. Am I doing things right so far?
Sending:
Intent intent = new Intent ( this, TaskListActivity.class );
intent.putExtra ( ProjectManager.ID, mId.toString () );
startActivity ( intent );
Recieving:
Intent intent = getIntent ();
mId = UUID.fromString ( intent.getStringExtra ( ProjectManager.ID ) );
add following code after intent:
Bundle extras = intent.getExtras();
String exampleString = extras.getString(ProjectManager.ID);
what is ProjectManager.ID?, you should pass same uniquekey while recieving data from putExtra even way of receiving data is wrong, check below code:
Sending:
Intent intent = new Intent ( this, TaskListActivity.class );
intent.putExtra (ProjectManager.ID, mId.toString () );
startActivity ( intent );
Recieving:
Bundle extras = intent.getExtras();
if(extras!=null){
String _Str = extras.getString(ProjectManager.ID);
}
Try This to Receive Extra:
Bundle extras = getIntent().getExtras();
String id;
if (extras != null) {
id= extras.getString(key);
}
In FirstActivity.java write these code.
Intent i = new Intent(FirstActivity.this,SecondActivity.class);
i.putExtra("KEY",value);
startActivity(i);
In SecondActivity.java:
Bundle extras=getIntent().getExtras();
String name=extras.getString("key"); //if data you are sending is String.
int i=extras.getInt("key"); //if data you are sending is integer.
To retrieve the extras in the new activity:
String valueOfExtra;
Intent i = getIntent();
//check first
if(i.hasExtra("extra1")){
valueOfExtra = i.getStringExtra("extra1");
}

Categories

Resources