Hello! I try to start activating in a separate thread with a period of time, the code is this: And the question itself: everything works, the activity starts, whether it is possible to make a separate method or how, so that you don’t write new Handler().postDelayed(new Runnable() { in each case new Handler().postDelayed(new Runnable() {

  arr_imageA[i].setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.startAnimation(anim); mediaPlayer.start(); playSample(R.raw.click_sound); switch (v.getId()) { case R.id.imageView1: new Handler().postDelayed(new Runnable() { @Override public void run() { startActivity(new Intent(getActivity(), a1.class)); } }, 100); break; case R.id.imageView2://todo копипаст startActivity(new Intent(getActivity(), a2.class)); break; case R.id.imageView3: startActivity(new Intent(getActivity(), a3.class)); break; } } }); 

    1 answer 1

    method

     public void startActivity(final Context activityContext, final Class<? extends Activity> activityClass) { new Handler().postDelayed(new Runnable() { @Override public void run() { startActivity(new Intent(activityContext, activityClass)); } }, 100); } 

    In your code, the only difference is that you launch different activites, so you need to put this part (different activites) as a parameter for the method. If you look at the constructor declaration for the class Intent

     public Intent(Context packageContext, Class<?> cls) 

    then we see that the second parameter is Class<?> cls , where <?> means any data type (that is, Class for any data type). Then we put this parameter into our own method and make it such a Class<? extends Activity> activityClass Class<? extends Activity> activityClass where <? extends Activity> <? extends Activity> means that we want to accept Сlass from any data type whose parent is Activity

    Well, then in the case we pass specific instances of Сlass of the corresponding activations.

     startActivity(getActivity(), a1.class); //и так далле 

    since the parent of any activity is Context, the above method call is valid

    • if not difficult, describe the principle of operation of this method, Thank you - upward
    • Added description - Yury Pashkov
    • one
      THANK YOU VERY MUCH! - upward