There is an activity with 7 buttons. Clicking on each button will open a new activity. The question is how to correctly register the method of processing keystrokes, the method indicates a specific activity

Intent intent = new Intent(this, Activity_Two.class); 

There is a question - you need to write 7 methods, for each activity, or you can write one method for all 7 buttons, but you must pass an object to the method parameters - Class. If the latter is possible, please tell me the syntax. private void setButtonBehavior(Class class){}

    2 answers 2

    To go from one activation to another 7, I would advise you to do something similar. In your main activity (MainActivity) to register the following:

     View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(View view) { switch (view.getId()) { case R.id.buttonFirst: start(FirstActivity.class); break; case R.id.buttonSecond: start(SecondActivity.class); break; ... ... case R.id.buttonSeven: start(SevenActivity.class); break; } } }; buttonFirst = (Button) findViewById(R.id.buttonFirst); buttonFirst.setOnClickListener(onClickListener); buttonSecond = (Button) findViewById(R.id.buttonSecond); buttonSecond.setOnClickListener(onClickListener); ... ... buttonSeven = (Button) findViewById(R.id.buttonSeven); buttonSeven.setOnClickListener(onClickListener); 

    And below in the same class, add the following method:

     private void start(Class activity) { startActivity(new Intent(MainActivity.this, activity)); } 

      You create a separate variable.

       View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View view) { Intent intent; switch view.getId(){ case R.id.button1: { intent = new Intent(this, Activity_One.class); break; } case R.id.button2: { intent = new Intent(this, Activity_Two.class); break } ................... ................... default: break; } } }; 

      And then you install it for the buttons:

       button1.setOnClickListener(listener); button2.setOnClickListener(listener); ....................... ........................