0

I am trying to create my first app in android studio, on the main screen there are three tick boxes asking the user which number of sides they want on the dice. I have a variable called sides which is set to 6,8 or 12 depending on which tick box the user ticks. I want the variable "sides" on the second activity so it can be used to generate a random integer between one and whatever "sides" is set to.

3 Answers3

1

In first activity Lets assume that you have button GO . When You clicks on Button GO it should start Second Activity say Activity2. Add following code to onClick of GO Button

Intent act2=new Intent(this,Activity2.class);//"this" is  activity reference
act2.putExtra("key",value);
startActivity(act2);

Now in the onCreate method of Activity2 you can retrieve value of key as follows:

Int key=getIntent().getIntExtra("key",0);//0 is default value

In the same way as done above you can pass value of "side" variable to next activity

VIjay J
  • 736
  • 7
  • 14
0

You can also save it in the Internal Storage and load it when you need, it is very useful because that way you can load it in every activity and every class you want.

you can learn how here. I recommend watching all three parts.

Ely Shaffir
  • 317
  • 3
  • 17
0

Easiest way is to use singleton classes.

public class DataHolder {


public int sides  = 0;

    private static DataHolder dataHolder = new DataHolder();

    public static DataHolder getInstance()
    {
        return dataHolder;
    }


} 

DataHolder.getInstance().sides=sideInActivityA;

you can access the variable by using int sideInActivityB = DataHolder.getInstance().sides;