user3041073
user3041073

Reputation: 33

Random number without duplicate when restart activity

I have activity A, B and C.

In activity B I have random number generator and Collections.shuffle(). This gives me random number in next activity C.

Then I use button to go back to activity B and restart whole process again, but it gives me duplicates in C since the whole Collections.shuffle() is restarted I guess.

I need to get random numbers without duplicates when I go from B to C, doing that repeatedly.

ACTIVITY B:

    public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);


Button a = (Button) findViewById(R.id.button1); // Here the R.id.button1 is the button from you design
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
    ArrayList<Integer> randomNumber = new ArrayList<Integer>();
    for (int i = 1; i <= 17; ++i) randomNumber.add(i);
    Collections.shuffle(randomNumber);
    int random = randomNumber.get(0); 
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("Value",random);
startActivity(intent);
}
    });

Button b = (Button) findViewById(R.id.button2); // Here the R.id.button1 is the button from you design
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
    ArrayList<Integer> randomNumber = new ArrayList<Integer>();
    for (int i = 18; i <= 35; ++i) randomNumber.add(i);
    Collections.shuffle(randomNumber);
    int random = randomNumber.get(0); 
    Intent intent=new Intent(MainActivity.this,Main2Activity.class);
    intent.putExtra("Value",random);
    startActivity(intent);
}
    });

Button c = (Button) findViewById(R.id.button3); // Here the R.id.button1 is the button from you design
c.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
    ArrayList<Integer> randomNumber = new ArrayList<Integer>();
    for (int i = 36; i <= 50; ++i) randomNumber.add(i);
    Collections.shuffle(randomNumber);
    int random = randomNumber.get(0); 
    Intent intent=new Intent(MainActivity.this,Main2Activity.class);
    intent.putExtra("Value",random);
    startActivity(intent);

}
    });

ACTIVITY C:

    public class Main2Activity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main2);

   Button back = (Button) findViewById(R.id.buttonback); // Here the R.id.button1 is the button from you design
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
odstevalnik.cancel();
Intent i = new Intent(Main2Activity.this, MainActivity.class);
startActivity(i);
}
    });

    Bundle bundle = getIntent().getExtras();
int random = bundle.getInt("Value");
TextView text = (TextView) findViewById(R.id.textView1);
if (random==1) {
    text.setText("blabla");
    image.setImageResource(R.drawable.img2);
    stopnja.setImageResource(R.drawable.stopnja1);
    Toast.makeText(getApplicationContext(), "blabla", 
    Toast.LENGTH_LONG).show();
}

   .....

Upvotes: 0

Views: 242

Answers (1)

null pointer
null pointer

Reputation: 5914

Create the collection of random number in the OnCreate of activity B. send the In this way the collection will not be restarted . send numbers from the list

int random = randomNumber.get(i++);

Upvotes: 1

Related Questions