我如何随机开始活动?

时间:2015-10-14 21:50:46

标签: android android-activity random

我正在寻找随机启动android studio中的活动而不重复。我希望能够通过单击按钮并在“if”语句中随机启动活动。谁能提供帮助就是太棒了!谢谢。

1 个答案:

答案 0 :(得分: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);
}

当用户点击按钮时,请致电openRandomActivity