-2

I was trying pass an int variable from one activity class(A) to another activity class(B). As I am a beginner, I am stuck here.

I have tried many methods but each time I run the code in debug mode , I can see that the int value which I have passed into the second class turns to 0.

Code for Activity A

   public void startMainActivity(View view)
   {
       //Sending the semester value to Navdrawer
       Intent intentsem = new Intent(LoginActivity.this, NavDrawer.class);
       intentsem.putExtra("UserSemester", usersemester);
       startActivity(intentsem);


       //Sending the branch value to Navdrawer

       Intent intentbranch= new Intent(LoginActivity.this, NavDrawer.class);
       intentbranch.putExtra("UserBranch", userbranch);
       startActivity(intentbranch);



   }

Code for Activity B

public void startSyllabusactivity(View view)

{
    // for first year

    int semester = getIntent().getIntExtra("UserSemester", 0);
    if (semester==1 || semester==2) {
        Intent l = new Intent(this, firstyear_syllabussubject_selector.class);
        startActivity(l);
    }


    //rest of the actions with branches to be added***********************

}

6 Answers6

2

You need to use Intent: Activity A:

doneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                 Intent intent = new Intent(context, ActivityB.class);
                 intent.putExtra("my_value", 2); //2 is the value you want to pass
                 intent.putExtra("UserBranch", userbranch);
                 startActivity(intent);

            }
        });

Activity B:

int variable = getIntent().getIntExtra("my_value", 0);//0 is the default value if the getintExtra doesn't find the "my_value"
int userBranch= getIntent().getIntExtra("UserBranch", 0);//0 is the default 
Chol
  • 2,097
  • 2
  • 16
  • 26
  • I can't use 'startActivity(intent);' as I don't want to start activity B as soon as the value is passed. There is a special dedicated DONE button for that purpose. How can I accomplish this? – Shimon Payyanadan Jul 26 '16 at 12:08
  • You put the code in the OnClick method of your done button – Chol Jul 26 '16 at 12:12
  • Still the value turns to be 0 when it reaches the second activity. I have updated the question with the code. – Shimon Payyanadan Jul 26 '16 at 14:40
  • In the code it seems that your are launching same activity two times – Chol Jul 26 '16 at 14:50
  • And in second one you are putting intentbranch.putExtra("UserBranch", userbranch); so try with "UserBranch" to get the int instead of "UserSemester" – Chol Jul 26 '16 at 14:51
  • It's working ! But how can I make it work with "UserSemester" ? I need both the values for processing. – Shimon Payyanadan Jul 26 '16 at 15:06
  • ok, you can pass two parameter in one intent, I have edited my answer – Chol Jul 26 '16 at 15:08
1

You can always use intent.putExtra() method, for example :

Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("myInt", myInt);
startActivity(intent);

and get it in ActivityB by :

Bundle extras = getIntent().getExtras();
if (extras != null) {
  int value = extras.getInt("myInt");
  //The key argument here must match that used in the other activity
}

Oh as a reminder, you should look up your question first on Google or directly at SO, as your question is a quite general one and you can dodge that negative vote..

Kevin Murvie
  • 2,592
  • 1
  • 25
  • 43
1

For this you are supposed to use Intent. You can get idea from below codes;

Suppose you have int variable as;

int x = 2;

Put this code in the Activity from where you want to go to another Activity:

Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("xVar", x);
startActivity(i);

In Second Activity use this code;

Intent i = getIntent();
int xVal = i.getIntExtra("xVar", 0);
vibhav yadav
  • 53
  • 1
  • 11
0
Intent myIntent = new Intent(FirstActivity.this,SecondActivity.class);
myIntent.putExtra("value", (int)100);
startActivity(myIntent);

in second activity

Bundle extras = getIntent().getExtras();
int value= extras.getInt("value");
0

In your current Activity, create a new Intent:

Intent i = new Intent(CurrentActivity.this, NewActivity.class);
i.putExtra("intValue",100);
startActivity(i);

Then in the new Activity, retrieve those values:

Bundle extras = getIntent().getExtras();
if (extras != null) {
  int value = extras.getInt("intValue");
  //The key argument here must match that used in the other activity
}

Use this technique to pass variables from one Activity to the other.

Nikhil PV
  • 1,014
  • 2
  • 16
  • 29
0

You can use Shared preferences to take care of this issue.

In Activity A:

int no=1;

SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

Editor editor = sharedpreferences.edit();

editor.putString("my_number", no+"");

editor.commit();

In Activity B:

SharedPreferences prefs = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);

String m_no = prefs.getString("my_number", null);

Usama Saleem
  • 424
  • 2
  • 4
  • 16