1

iam trying to switch from say activity 1 to activity 2 when a button is clicked. and ive got how to do that. what i cant figure out is how to get it so 50% of the time it goes to activity 2 and the other 50% to activity 3. iam sorry i know its a very obvious question. iam new to this so could you please explain a little and also similar effects like switching to completely randomn out of say 5 different specified activites. thanks in advance ... intent code below. please explain using my code if possible.

 yes.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent yes1=new Intent(MainActivity.this,Question2.class);
            startActivity(yes1);
Ankit
  • 6,554
  • 6
  • 49
  • 71
user2643867
  • 11
  • 1
  • 7
  • can you post more of your code,coz from this much we cant say anything – karan Aug 01 '13 at 22:05
  • Your code shows `Question2.class` which suggests it may be a quiz / game or similar. Why would you define an `Activity` for each question? It seems it would be easier to have a single 'question' `Activity` and either pass it a random number as an `Intent` extra or have that `Activity` pick its own random number when it is started. – Squonk Aug 01 '13 at 22:58

4 Answers4

1

Off the top of my head, so it might not compile completely :-) but the idea is sound

List<Class<? extends Activity>> activities = new ArrayList<Class<? extends Activity>>();
activities.add(Question.class);
activities.add(Question2.class);

Random generator = new Random();
Class<? extends Activity> randomActivity = activities.get(generator.nextInt(activities.size()));

Intent intent = new Intent(this, randomActivity);
startActivity(intent);
Blundell
  • 75,855
  • 30
  • 208
  • 233
  • Just curious, why not just `List`? – ssantos Aug 02 '13 at 08:30
  • because then you are holding references to the Activity, which hold a reference to Context, which would hold everything in memory. So if you say made that list static or held onto it some other way you can get memory leaks. Memory leaks are baad. – Blundell Aug 02 '13 at 08:43
  • Mm good to know :) In other things, `nextInt(i)` already returns a number between `0` and `i-1`, so you can safely ask for `nextInt(activities.size())` – ssantos Aug 02 '13 at 08:52
  • Ah yeah I wasn't sure so just to be safe I added -1, I'll edit and remove – Blundell Aug 02 '13 at 08:56
1

You could just use Random class to decide which will be your next intent. Please, take this code snippet as an example.-

Intent newIntent = null;
Random rand = new Random();

int index = rand.nextInt(4);
switch (index) {
    case 0:
        newIntent = new Intent(this, Question1.class);
    break;
    case 1:
        newIntent = new Intent(this, Question2.class);
    break;
    case 2:
        newIntent = new Intent(this, Question3.class);
    break;
    case 3:
        newIntent = new Intent(this, Question4.class);
    break;
}

startActivity(newIntent);
ssantos
  • 16,001
  • 7
  • 50
  • 70
  • 1
    You're right, just a simple example for the scenario in the question. Of course, for a large number of intents an array is a must :) – ssantos Aug 02 '13 at 07:44
  • Also add a `default: throw new RuntimeException("Unrecognised Index");` to be safe. This will help you if you catch cases of increasing the rand and forgetting to add the Intent – Blundell Aug 02 '13 at 10:39
0

Try this:

yes.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View arg0) {
       double rand = Math.random();
       if(rand < 0.5){
           Intent yes1 = new Intent(MainActivity.this, Question2.class);
           startActivity(yes1);
       } else {
           Intent yes2 = new Intent(MainActivity.this, Question3.class);
           startActivity(yes2);
       }
   }
});

Similar for random switching between 5 different activities. You could probably optimize it so you're not just if branching between all your different options if you have so many different activities.

Catherine
  • 13,588
  • 9
  • 39
  • 60
  • yeah but look at the code duplication, there are better answers. Especially when you get to wanting 5 different activities – Blundell Aug 01 '13 at 22:14
  • @Blundell I think I covered that possibility with "You could probably optimize it so you're not just branching between all your different options if you have so many different activities." You can certainly create an array of your options and index into it or something, I was just going for a quick bit of code. – Catherine Aug 01 '13 at 22:22
  • Oh I know, it's an option, just wanted to point out to the Q not to jump on the first answer posted – Blundell Aug 01 '13 at 22:22
  • well thanks guys .. i considered all options ... and iam going to try all decide for the simplest and best fitting to my app. thanks to all asnwers !! appreciate it – user2643867 Aug 01 '13 at 22:47
  • it doesnt seem to work for more than 2 ... it seems to be a problem for "else" thing – user2643867 Sep 10 '13 at 14:45
  • It won't work for more than 2 unless you change the values that you're comparing `rand` to. – Catherine Sep 10 '13 at 21:12
0

You could set up a String Array of the class names like

String[] classes = new String[3];
classes[0] = Activity1;
classes[1] = Activity2;
classes[2] = Activity3;

Then use a Random number to pick between them and on click do something like

 @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Random r = new Random();
        int number = r.nextInt(4);
        String curClass = classes[number);
        Intent yes1=new Intent(MainActivity.this,Class.forName(curClass));
        startActivity(yes1);
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Uses reflection will be slower, probably not the best idea – Blundell Aug 01 '13 at 22:13
  • @Blundell I've never had a problem with it but I will check into it. Thanks for your comment. – codeMagic Aug 01 '13 at 22:15
  • http://stackoverflow.com/a/435568/413127 and bear in mind the scores are on PC systems where as we deal with the mobile world – Blundell Aug 01 '13 at 22:21
  • @Blundell Thanks for the resource! I can see how this may not be the most optimized but I'm wondering if you never have an extreme amount of `Activities` if it will make a noticeable difference. – codeMagic Aug 01 '13 at 22:27
  • No it probably won't make a noticeable difference tbh, but it is a slippery slope to start from :-) – Blundell Aug 02 '13 at 10:40