2

i want to pass integer value from one activity to other i try to do this but value not pass please tell me right solution.

this the activity code from where i want to pass value to other activity.

Intent intent = new Intent(getApplicationContext(),
                            TotalCalories.class);
                    intent.putExtra("Total Sum", sum);
                    startActivity(intent);

this is the code where i want to get the value.

  Bundle extras = getIntent().getExtras();
    int sum = Integer.parseInt(extras.getString("Total Sum"));
    TextView textview = (TextView) findViewById(R.id.textViewname);
    textview.setText(extras.getString("Total Sum"));
Eman Ch
  • 192
  • 2
  • 10

6 Answers6

2

From sender activity:

Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);

In receiver activity:

 Intent mIntent = getIntent();
 int intValue = mIntent.getIntExtra("intVariableName", 0);

Note: The 2nd parameter received '0', is default value. If you don't pass any value through intent then it will assign 0 to the intValue variable.

Mohammad Arman
  • 7,020
  • 2
  • 36
  • 50
2

Try like this change the Totalsum in your get String

 Intent intent = new Intent(getApplicationContext(),
                                    TotalCalories.class);
                            intent.putExtra("TotalSum", sum);
                            startActivity(intent);

Add this inside Oncreate of new activity

  Bundle extras = getIntent().getExtras();
            int sum = Integer.parseInt(extras.getString("TotalSum"));
            TextView textview = (TextView) findViewById(R.id.textViewname);
            textview.setText("Your OutPut"+sum);
Android Dev
  • 421
  • 8
  • 26
1

Use following in another activity to get integer

Intent i =getIntent();
int abc=  i.getIntExtra("TotalSum");
TextView textview =   (TextView)findViewById(R.id.textViewname);
textview.setText(String.valueOf(abc));
Suraj
  • 737
  • 6
  • 21
0

two issues in your code

1) you are not passing bundle but fetching value from bundle.

use below link How to pass a value from one Activity to another in Android?

2) and to get integer value

Intent in =getIntent();
int abc=  in.getIntExtra("value");
Community
  • 1
  • 1
Amol Sawant
  • 13,842
  • 2
  • 20
  • 27
0

This is how you do it

int yourInt = 0; //initialize it

yourInt = yourIntergerValue;

Intent yourActivityIntent = new Intent(getApplicationContext(),YourActivity.class); 
yourActivityIntent.putExtra("yourIntKey",yourInt); 
startActivity(yourActivityIntent);
Delimitry
  • 2,987
  • 4
  • 30
  • 39
0

First put the value in FirstActivity

       Intent intent = new Intent(this, yourSecondActivity.class);
       intent.putExtra("intValue",value);
       startActivity(intent);

then, SecondActivity get the value

        Bundle exBundle= getIntent().getExtras();
        int intValue= exBundle.getInt("intValue");
        TextView textview = (TextView) findViewById(R.id.textViewname);
        textview.setText("Your Value is "+intValue);
jeet parmar
  • 868
  • 8
  • 19