Start Activities Randomly in Android

我是研究僧i 提交于 2020-01-03 01:44:24

问题


I have 9 Activities (It can be increased more than this not a fixed value) in my App on Click of the Button i want to start activities randomly.Like

Activity 1 -> Activity ->3 and so on.

And if the user clicks on the back button i want to finish the current activity and start again a new activity the the user haven't been visited yet ..

So how could i do this .

For random activity i have tried this way

Random rnd = new Random();
int x=rnd.nextInt(9)+1;
Intent myIntent = new Intent();
switch(x){
case 1:
myIntent.setClass(view.getContext(),Scrn1.class);
break;
case 2:
myIntent.setClass(view.getContext(), Scrn2.class);
break;
case 3:
myIntent.setClass(view.getContext(), Scrn1.class);
break;
}

But i am not getting the way how could i start a new activity that the user haven't visited yet


回答1:


I used it:

    Button button1 = (Button)findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Random rnd = new Random();
            int x=rnd.nextInt(9)+1;

            switch(x){
            case 1:
                Intent intent=new Intent(v.getContext(), Scrn1.class);
                 startActivityForResult(intent1, 0);
            break;
            case 2:
                Intent intent1=new Intent(v.getContext(), Scrn2.class);
                 startActivityForResult(intent2, 0);
            break;


            ...


            case 8:
                Intent intent2=new Intent(v.getContext(), Scrn8.class);
                 startActivityForResult(intent8, 0);
            break;
            case 9:
                Intent intent2=new Intent(v.getContext(), Scrn9.class);
                 startActivityForResult(intent9, 0);
            break;
            }  
        }  });



回答2:


The easiest way is to use SharedPreferences to keep track of started activities. For example, you can store array with class names or some unique activity's id. Before starting new activity you read array, see what was started already, choose new activity class, update array in preferences and launch new activity.




回答3:


You should handle onbackpress method,in method finish your present activity and call your random activity function, It will redirect..




回答4:


Keep a arraylist which keeps the activities number which is already launched. so next time if your random number is from that list just discard and generate the number again.

Your code looks good, you need to integrate this logic




回答5:


May be this could work for you. Suppose there are 4 activities.

Random rnd = new Random();
int[] activitiesVisited = new int[] {0,0,0,0};
int x=rnd.nextInt(9);
while(activitiesvisited[x] == 1) {
    x=rnd.nextInt(9);
}

Intent myIntent = new Intent();
switch(x){
case 1:
myIntent.setClass(view.getContext(),Scrn1.class);
activitiesVisited[0] = 1;
break;
case 2:
myIntent.setClass(view.getContext(), Scrn2.class);
activitiesVisited[1] = 1;
break;
case 3:
myIntent.setClass(view.getContext(), Scrn3.class);
activitiesVisited[2] = 1;
break;
case 4:
myIntent.setClass(view.getContext(), Scrn4.class);
activitiesVisited[3] = 1;
break;
}


来源:https://stackoverflow.com/questions/20113947/start-activities-randomly-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!