I would like to pass a new value for an integer from one Activity to another.
i.e.:
Activity B contains an
integer[] pics = { R.drawable.1, R.drawable.2, R.drawable.3}
I would like activity A to pass a new value to activity B:
integer[] pics = { R.drawable.a, R.drawable.b, R.drawable.c}
So that somehow through
private void startSwitcher() {
Intent myIntent = new Intent(A.this, B.class);
startActivity(myIntent);
}
I can set this integer value.
I know this can be done somehow with a bundle, but I am not sure how I could get these values passed from Activity A to Activity B.
It's simple. On the sender side, use Intent.putExtra:
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);
On the receiver side, use Intent.getIntExtra:
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
Their are two methods you can use to pass an integer. One is as shown below.
A.class
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);
B.class
Intent intent = getIntent();
int intValue = intent.getIntExtra("intVariableName", 0);
The other method converts the integer to a string and uses the following code.
A.class
Intent intent = new Intent(A.this, B.class);
Bundle extras = new Bundle();
extras.putString("StringVariableName", intValue + "");
intent.putExtras(extras);
startActivity(intent);
The code above will pass your integer value as a string to class B. On class B, get the string value and convert again as an integer as shown below.
B.class
Bundle extras = getIntent().getExtras();
String stringVariableName = extras.getString("StringVariableName");
int intVariableName = Integer.parseInt(stringVariableName);
In Activity A
private void startSwitcher() {
int yourInt = 200;
Intent myIntent = new Intent(A.this, B.class);
intent.putExtra("yourIntName", yourInt);
startActivity(myIntent);
}
in Activity B
int score = getIntent().getIntExtra("yourIntName", 0);
In Sender Activity Side:
Intent passIntent = new Intent(getApplicationContext(), "ActivityName".class);
passIntent.putExtra("value", integerValue);
startActivity(passIntent);
In Receiver Activity Side:
int receiveValue = getIntent().getIntExtra("value", 0);
Related
Why does this throw NullPointerException ?
In Redeem.java
int yourInt = 200;
Intent myIntent = new Intent(Redeem.this, MainActivity.class);
myIntent.putExtra("intVariableName", yourInt);
startActivity(myIntent);
In MainActivity.java
Bundle extras = getIntent().getExtras();
int score = extras.getInt("intVariableName");
Try with:
Bundle extras = getIntent().getExtras();
String stringScore = extras.getString("intVariableName");
int score = Integer.parseInt(stringScore);
Or:
int score = intent.getIntExtra("intVariableName", 0);
You can also try this:
int yourInt = 200;
Intent myIntent = new Intent(Redeem.this, MainActivity.class);
myIntent.putExtra("intVariableName", String.valueOf(yourInt));
startActivity(myIntent);
In MainActivity.java
Bundle extras = getIntent().getExtras();
int score =Integer.parseInt(extras.getString("intVariableName"));
Use this:
getIntent().getIntExtra("intVariableName", 0); //where 0 is default value
I'm new in programming and I recently encountered a problem in activities. I couldn't pass my int array to multiple activities.
Here is my First Activity :
public void Start(View view) {
Intent i = new Intent(this, Situation1.class);
i.putExtra("arr", new int[]{0, 0, 0, 0, 0, 0});
startActivity(i);
}
Here is my Situation1:
public void Serious(View view)
{
Intent intent = new Intent(this, Situation2.class);
Intent i = getIntent();
int[] arr = getIntent().getIntArrayExtra("arr");
arr[2]=arr[2]+1;
arr[1]=arr[1]+1;
startActivity(intent);
}
Situation2:
public void India(View view)
{
Intent intent = new Intent(this, Situation3.class);
Intent i = getIntent();
Bundle b = i.getExtras();
int[] arr=i.getIntArrayExtra("arr");
arr[0]=arr[0]+1;
startActivity(intent);
}
Thank you for the help!
In Situation1 and Serious function, you didn't put your array in intent which you start the next activity with.
public void Serious(View view)
{
Intent intent = new Intent(this, Situation2.class);
Intent i = getIntent();
int[] arr = getIntent().getIntArrayExtra("arr");
arr[2]=arr[2]+1;
arr[1]=arr[1]+1;
intent.putExtra("arr", arr);
startActivity(intent);
}
You can use arraylist of integer like this:
This code sends array of integer values
Initialize array List
List<Integer> test = new ArrayList<Integer>();
Add values to array List
test.add(1);
test.add(2);
test.add(3);
Intent intent=new Intent(this, targetActivty.class);
Send the array list values to target activity
intent.putIntegerArrayListExtra("test", (ArrayList<Integer>) test);
startActivity(intent);
here you get values on targetActivty
Intent intent=getIntent();
ArrayList<String> test = intent.getStringArrayListExtra("test");
For more information:
http://stackoverflow.com/questions/3848148/sending-arrays-with-intent-putextra
While passing:
Bundle b=new Bundle();
b.putIntArray("arr", new int[]{0, 0, 0, 0, 0, 0});
Intent i = new Intent(this, Situation1.class);
i.putExtras(b);
startActivity(i);
While getting (most probably in oncreate of Situation1 activity):
Bundle b=this.getIntent().getExtras();
int[] array=b.getIntArray("arr");
I've done the coding as below...
Activity1
Intent intent=new Intent(Activity1.this, Activity2.class);
intent.putExtra("R", num);
startActivity(intent);
Activity 2
v=getIntent().getIntExtra("R",2);
the value v should be passed to a switch.. v has 4 values...
But value of v is not changing.. always the same.. anything wrong about code ?
In Activity 1:-
Intent intent=new Intent(Activity1.this, Activity2.class);
intent.putExtra("R", "anyvalue");
startActivity(intent);
In Activity2:-
Public String r;
in OnCreate()
Bundle b = getIntent().getExtras();
r = b.getString("R");
Then again use Intent on Button click
Intent intent=new Intent(Activity2.this, Activity3.class);
intent.putExtra("rr", r);
startActivity(intent);
In Activity3 :-
Public String s;
in onCreate()
Bundle b = getIntent().getExtras();
s = b.getString("rr");
Activity1:
Intent intent=new Intent(Activity1.this, Activity2.class);
intent.putExtra("R",String.valueOf(num));
startActivity(intent);
Activity 2:
Intent intent = getIntent();
v= Integer.parseInt(intent.getStringExtra("R"));
Activity 1
String numstr=num.toString();
Intent intent=new Intent(Activity1.this, Activity2.class);
intent.putExtra("R", numstr);
startActivity(intent);
Activity 2
String vstr=getIntent().getExtras().getString("R");
int v=Integer.parseInt(vstr);
The logic here is simple.Just convert the integer to string and pass it.Then on the other class recieve it as string and then convert it into integet using parseInt().
In Acitvity 1
Intent pass = new Intent(Activity2.this);
Bundle data = new Bundle();
data.putInt("pos", position);
pass.putExtras(data);
startActivity(pass);
In Activity 2 in your on create method
Bundle extras = getIntent().getExtras();
if(extras != null){
position = extras.getInt("pos", position);
}
I am having array of image URL.On button click I want to send selected image URL to another activity.
Main.java
String[] imageUrl={"https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png","https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png", "https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png"};
Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
btnNextScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse("******");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent)
}
});
OpenImage.java
ImageView image = (ImageView)findViewById(R.id.imageview);
What to write here next
In your First Activity,
String[] data = {"Hello", "World"};
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("some_key", data);
startActivity(intent);
Then in you Sencon Activity,
// At class level
private static final String TAG = SecondActivity.class.getSimpleName();
// In onCreate
String[] data = getIntent().getExtras().getStringArray("some_key");
for (String x : data) {
Log.i(TAG, x);
// Toast to display all you values one by one
Toast.makeText(SecondActivity.this, x, Toast.LENGTH_SHORT).show();
}
Hope this helps...:)
Try This:
Bundle bundel = new Bundle();
bundel.putStringArray("key",array);
Intent intent = new Intent(this,next.class)
intent.putExtras(bundel);
startActivity(intent);
or just
intent.putExtra("strings", myStrings);
the putExtra has many overloads, pass array of primitive type is one of them :)
Use This ti send another Activity...
Intent intent1 = new Intent(Intent.ACTION_VIEW, uri);
Bundle bundle = new Bundle();
bundle.putStringArray("ArrayURL", imageUrl);
intent1.putExtras(bundle);
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent1);
and For Getting
Bundle b = getArguments();
Cat_Name = b.getStringArray("ArrayURL");
i pass int to next activity using this code
Intent intent = new Intent(A.this, B.class);
intent.putExtra("selectedType", i);
startActivity(intent);
and then receive this in activity B
Intent intent = new Intent();
int i = intent.getIntExtra("selectedType", 0);
Toast.makeText(getApplicationContext(), String.valueOf(i),
Toast.LENGTH_LONG).show();
but when in this activity, it always display 0.
Intent intent = new Intent();
You are creating a new intent instead of using the one passed to your ActivityB. So use
Intent intent = getIntent();
instead;
use this int i = getIntent().getIntExtra("selectedType", 0);
try getIntent().getExtras().getInt("selectedType")
Try now,
int value = getIntent().getExtras().getInt("selectedType");
Intent intent = new Intent(A.this, B.class);
intent.putExtra("selectedType", i);
startActivity(intent);
Intent intent = new getIntent();
^^^^^^^^^
int i = intent.getIntExtra("selectedType", 0);
Toast.makeText(getApplicationContext(), String.valueOf(i),
Toast.LENGTH_LONG).show();
Because you're creating a new intent and trying to get "selectedType" on it. But that intent has just been created, so it hasn't that value that you seek.
Try to getIntent() method to get your calling intent, which has your "selectedType" value...
Here's a snap:
Bundle extras = getIntent().getExtras();
if(extras != null) {
int value = extras.getIntExtra("selectedType", 0);
Toast.makeText(getApplicationContext(), String.valueOf(value), Toast.LENGTH_LONG).show();
}
and then receive this in activity B
Intent intent = new Intent();
int i = intent.getIntExtra("selectedType", 0);
This is wrong. You are creating a new intent object. To get the intent object that was used to start this activity use getIntent() method.
Intent intent = getIntent();
int i = intent.getIntExtra("seelctedType", 0);
Intent intent = new Intent(A.this, B.class);
intent.putExtra("selectedType",i);
startActivity(intent);
And receiving..
if (getIntent().getExtras().containsKey("selectedType")) {
int message = getIntent().getIntExtra("selectedType");
Toast.makeText(ReceiverActivity.this, "" + message, Toast.LENGTH_LONG)
.show();
}