I'm looking to randomly start activities in android studio without repetition. I'd like to be able to start an activity randomly by clicking a button and in an 'if' statement. Whoever can help out that would be fantastic! Thank you.
我想在android studio中随机启动活动而不重复。我希望能够通过单击按钮并在“if”语句中随机启动活动。谁能提供帮助就是太棒了!谢谢。
1 个解决方案
#1
2
ArrayList<Class> activities = new ArrayList<>();
@Override
public void onCreate(Bundle savedInstanceState) {
//your codes...
activities.add(History.class);
activities.add(Bookmark.class);
activities.add(Themes.class);
//your codes
}
public void openActivity(Class class_) {
Intent intent = new Intent(this, class_);
startActivity(intent);
}
public void openRandomActivity(){
Class that = activities.get(new Random().nextInt(activities.size()));
activities.remove(that); // after using, remove from list
openActivity(that);
}
Call openRandomActivity
when a user click to button.
用户单击按钮时调用openRandomActivity。
#1
2
ArrayList<Class> activities = new ArrayList<>();
@Override
public void onCreate(Bundle savedInstanceState) {
//your codes...
activities.add(History.class);
activities.add(Bookmark.class);
activities.add(Themes.class);
//your codes
}
public void openActivity(Class class_) {
Intent intent = new Intent(this, class_);
startActivity(intent);
}
public void openRandomActivity(){
Class that = activities.get(new Random().nextInt(activities.size()));
activities.remove(that); // after using, remove from list
openActivity(that);
}
Call openRandomActivity
when a user click to button.
用户单击按钮时调用openRandomActivity。