随机activity不重复

Random activity without repetition

我已经在这里阅读了数百篇关于它的帖子,但 none 适合我的简单需求。 菜鸟警报

我正在尝试在我的应用程序中发出通知,从活动列表中选择并随机打开其中一个但不重复,我能够产生随机效果,但有些活动会弹出多次而其他活动不会't.

所以基本上我需要的是随机检查列表中的所有活动,当所有活动都被访问后,重新洗牌并重新开始

这是我编造的一个小例子:一个按钮带我进行 10 项活动:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button randomBtn;
        randomBtn = (Button) findViewById(R.id.randomBtn);
        randomBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ArrayList<Class> activities = new ArrayList<>();
                activities.add(Activity01.class);
                activities.add(Activity02.class);
                activities.add(Activity03.class);
                activities.add(Activity04.class);
                activities.add(Activity05.class);
                activities.add(Activity06.class);
                activities.add(Activity07.class);
                activities.add(Activity08.class);
                activities.add(Activity09.class);
                activities.add(Activity10.class);

                Collections.shuffle(activities);

                Intent intent = new Intent(MainActivity.this, activities.get(0));
                startActivity(intent);

            }
        });
    }
}

结果是这样的: 第一个 运行: 04, 07, 07, 05, 03, 09, 06, 09, 04, 01

第二个运行: 07, 04, 10, 02, 05, 06, 10, 05, 02, 08.

我就是找不到它。

提前致谢。

看,伙计,试试这样的东西:

            ArrayList<Class> activities = new ArrayList<>();
            activities.add(Activity01.class);
            activities.add(Activity02.class);
            activities.add(Activity03.class);
            activities.add(Activity04.class);
            activities.add(Activity05.class);
            activities.add(Activity06.class);
            activities.add(Activity07.class);
            activities.add(Activity08.class);
            activities.add(Activity09.class);
            activities.add(Activity10.class);

Button randomBtn;
    randomBtn = (Button) findViewById(R.id.randomBtn);
    randomBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(activities.size == 0){
             //create a method that instantiate again the Activities and add to the list;
            }
            Collections.shuffle(activities);

            Activity activityToShow = activities.get(0);

            Intent intent = new Intent(MainActivity.this, activityToShow);
            activities.remove(0);
            startActivity(intent);

        }
    });

为了将来参考,这是我使用的代码,感谢@PedroFernandes,适用于我的情况:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ArrayList<Class> activities = new ArrayList<>();
        activities.add(Activity01.class);
        activities.add(Activity02.class);
        activities.add(Activity03.class);
        activities.add(Activity04.class);
        activities.add(Activity05.class);
        activities.add(Activity06.class);
        activities.add(Activity07.class);
        activities.add(Activity08.class);
        activities.add(Activity09.class);
        activities.add(Activity10.class);

        Button randomBtn;
        randomBtn = (Button) findViewById(R.id.randomBtn);
        randomBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (activities.size()==0){
                    activities.add(Activity01.class);
                    activities.add(Activity02.class);
                    activities.add(Activity03.class);
                    activities.add(Activity04.class);
                    activities.add(Activity05.class);
                    activities.add(Activity06.class);
                    activities.add(Activity07.class);
                    activities.add(Activity08.class);
                    activities.add(Activity09.class);
                    activities.add(Activity10.class);
                }
                Collections.shuffle(activities);

                Class activityToShow = activities.get(0);

                Intent intent = new Intent(MainActivity.this, activityToShow);
                activities.remove(0);
                startActivity(intent);
            }
        });
    }
}