https://stackoverflow.com/questions/53941826/show-random-page
In the onCreate() method of your activity:
  • create a list containing the classes of the activities to open
  • create a list of shuffled numbers beginning from 0 to the size-1 of the previous list
  • initialize a variable that will iterate through the list of numbers, each time picking the next.
  • set the listener of the button
  • val activities = listOf(
        MainActivity1::class.java,
        MainActivity2::class.java,
        MainActivity3::class.java,
        MainActivity4::class.java)
    
    val numbers = (0 until activities.size).shuffled()
    
    var counter = 0
    
    button.setOnClickListener {
        if (counter >= activities.size) counter = 0;
        val intent = Intent(this@YourActivity, activities[numbers[counter]])
        startActivity(intent)
        counter++
    }
    
If you want to stop opening any activity after they are all opened,
change the if statement inside the listener to:
if (counter >= activities.size) {
    // show a toast ???
    return;
} 

0  0
 
Created:


| | Code Snipit Repository