I have 5 activities. What I want to happen is to open random activities when the Start button is clicked on the MainMenu.
For Example: Activity 1 -> Activity 4 -> Activity 3
I have tried the codes posted here and here but none of them worked.
This is my code in Start Button
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 would be a big help. Thanks in advance!
@Farbod Salamat-Zadeh your solution is great, except that part
would result in mistake in Android Studio 2.1. This should instead be
To start an activity, you need to use intents. And you can call this when a button is clicked like so:
To make this random, we need to change it slightly so it is more like this:
You can read more about starting activities and using intents here if you like.
Updated answer to updated question/comment:
If you don't want to open an Activity that has already been opened, that would be a little more complex.
In your main activity add the code below (it is almost the same as the one in the previous answer, but a little different):
In all other 5 activities, use the code below: