android - Open random activities when button is clicked -
android - Open random activities when button is clicked -
i have 5 activities. want happen open random activities when start button clicked on mainmenu.
for example: activity 1 -> activity 4 -> activity 3
i have tried codes posted here , here none of them worked.
this code in start button
class="snippet-code-js lang-js prettyprint-override"> gotomenu=(button)findviewbyid(r.id.btnstart); gotomenu.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub startactivity(new intent(mainmenu.this, menu_welcomeluzon.class)); overridependingtransition(r.animator.transition_fade_in, r.animator.transition_fade_out); } });
any code snippets big help. in advance!
to start activity, need utilize intents. , can phone call when button clicked so:
button mybutton = (button) findviewbyid(r.id.my_button); mybutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(currentactivity.class, nextactivity.class); startactivity(intent); }
to create random, need alter more this:
button mybutton = (button) findviewbyid(r.id.my_button); mybutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // here, generating random number random generator = new random(); int number = generator.nextint(5) + 1; // '5' number of activities class activity = null; // here, checking see output of random switch(number) { case 1: // e.g., if output 1, activity open activityone.class activity = activityone.class; break; case 2: activity = activitytwo.class; break; case 3: activity = activitythree.class; break; case 4: activity = activityfour.class; break; default: activity = activityfive.class; break; } // utilize intents start activities intent intent = new intent(getbasecontext(), activity); startactivity(intent); } }
you can read more starting activities , using intents here if like.
updated reply updated question/comment:if don't want open activity has been opened, little more complex.
in main activity add together code below (it same 1 in previous answer, little different):
button mybutton = (button) findviewbyid(r.id.my_button); mybutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // creating list, store activities haven't been opened yet arraylist<class> activitylist = new arraylist<>(); activitylist.add(activityone.class); activitylist.add(activitytwo.class); activitylist.add(activitythree.class); activitylist.add(activityfour.class); activitylist.add(activityfive.class); random generator = new random(); int number = generator.nextint(5) + 1; class activity = null; // here, checking see output of random switch(number) { case 1: activity = activityone.class; // adding number of activity list activitylist.remove(activityone.class); break; case 2: activity = activitytwo.class; activitylist.remove(activitytwo.class); break; case 3: activity = activitythree.class; activitylist.remove(activitythree.class); break; case 4: activity = activityfour.class; activitylist.remove(activityfour.class); break; default: activity = activityfive.class; activitylist.remove(activityfive.class); break; } // utilize intents start activities intent intent = new intent(getbasecontext(), activity); // `intent.putextra(...)` used pass on info next activity intent.putextra("activity_list", activitylist); startactivity(intent); } }
in other 5 activities, utilize code below:
button mybutton = (button) findviewbyid(r.id.another_button); mybutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { arraylist<class> activitylist = new arraylist<>(); bundle extras = getbasecontext().getintent().getextras(); activitylist = extras.get("activity_list"); if(activitylist.size() == 0) { // when after activities have been opened dosomeaction(); } else { // now, random number generated between 1 , many // activities have remaining random generator = new random(); int number = generator.nextint(activitylist.size()) + 1; class activity = null; // here, checking see output of random switch(number) { case 1: // open first remaining activity of list activity = activitylist.get(0); // remove activity list activitylist.remove(0); break; case 2: // open sec remaining activity of list activity = activitylist.get(1); activitylist.remove(1); break; case 3: // open 3rd remaining activity of list activity = activitylist.get(2); activitylist.remove(2); break; case 4: // open 4th remaining activity of list activity = activitylist.get(3); activitylist.remove(3); break; default: // open 5th remaining activity of list activity = activitylist.get(4); activitylist.remove(4); break; } // note: in above, might not have 3 remaining activities, example, // doesn't matter because case wouldn't called anyway, // have decided number between 1 , number of // activities left. // starting activity, , passing on remaining number of activities // next 1 opened intent intent = new intent(getbasecontext(), activity); intent.putextra("activity_list", activitylist); startactivity(intent); } } }
android random
Comments
Post a Comment