1

I have a problem with passing int variable to another class. I've tried many methods of passing this variable but none have worked. Help me pass variable randomNum from MainActivity to HistoryActivity. Thanks!

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    ........

        int min, max;
        max = 1;
        min = 0;
        int randomNum = min + (int) (Math.random() * ((max - min) + 1));


        int backgroundImages[] = {R.drawable.background1,
                R.drawable.background2};
        final Drawable backgroundImage = getResources().getDrawable(
                backgroundImages[randomNum]);
        linearLayoutBackground.setBackgroundDrawable(backgroundImage);



        // // WHAT TO DO NOW POGA
        buttonWhatToDoNow.setOnClickListener(new View.OnClickListener() {

                .........    

            }

        });


        buttonHistory.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent i = new Intent(MainActivity.this, HistoryActivity.class);
                startActivity(i);
                finish();

            }
        });    
    }
}

public class HistoryActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

            .......

        randomNum =..............;

        int backgroundImages[] = {R.drawable.background1,
                R.drawable.background2};
        final Drawable backgroundImage = getResources().getDrawable(
                backgroundImages[randomNum]);
        linearLayoutBackground.setBackgroundDrawable(backgroundImage);
    }

}
Eric
  • 95,302
  • 53
  • 242
  • 374
user1816780
  • 121
  • 4
  • 14
  • http://stackoverflow.com/questions/15859445/how-do-you-pass-a-string-from-one-activity-to-another/15859488#15859488. you can use intents to pass data between activities. – Raghunandan Jun 07 '13 at 12:28

3 Answers3

1
Intent i = new Intent(MainActivity.this, HistoryActivity.class);
i.putExtra("intVariableName", randomNum);
startActivity(i);

HISTORY ACTIVITY in on create method for example

 Intent mIntent = getIntent();
 int intValue = mIntent.getIntExtra("intVariableName", 0);
Marko Niciforovic
  • 3,561
  • 2
  • 21
  • 28
0

lets pretend that we have 2 classes and 2 activities.

if you want your value to be in the HistoryActivity just declare the value in the class of the Historyactivity(which is for yo the second page) and make an intent from first activity to the historyactivity....that's it !!!!

R.F
  • 312
  • 1
  • 16
0

In your onclick :

    //onclick MainActivity
    Bundle bundle = new Bundle();
    bundle.putInt("key", randomInt);
    Intent intent = new Intent(MainActivity.this, HistoryActivity.class);
    intent.putExtras(bundle);
    finish();
    startActivity(intent);

    //then in your HistoryActivity onCreate
    int randomNum = getIntent().getExtras().getInt("key");`
Lunchbox
  • 1,538
  • 2
  • 16
  • 42