Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass integer from one Activity to another?

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.

like image 355
benbeel Avatar asked Aug 16 '11 05:08

benbeel


People also ask

How do you transfer an integer from one activity to another?

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.

How do you pass a value from one activity to another?

Using Intents This example demonstrate about How to send data from one activity to another in Android using intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How can I get integer value from one activity to another in Android?

//First Activity Intent i = new Intent(this, SecondActivity. class); i. putExtra("MY_KEY", 15); startActivity(i); //Second Activity int number = getIntent(). getExtras().

What is intent putExtra in Android?

Intents are asynchronous messages which allow Android components to request functionality from other components of the Android system. For example an Activity can send an Intents to the Android system which starts another Activity . putExtra() adds extended data to the intent.


1 Answers

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); 
like image 169
Paresh Mayani Avatar answered Sep 23 '22 21:09

Paresh Mayani