Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try it like this:</p> <p>(just change "btnOpenA.setOnClickListener(btnMethods);" and "btnOpenB.setOnClickListener(btnMethods);" for this two and forget the button class)</p> <hr> <pre><code> btnOpenA.setOnClickListener(new OnClickListener() { @Override public void onClick(final View view) { final Intent intent = new Intent(MainActivity.this, ActivityA.class); SensorActivity.this.startActivity(intent); } }); btnOpenB.setOnClickListener(new OnClickListener() { @Override public void onClick(final View view) { final Intent intent = new Intent(MainActivity.this, ActivityB.class); SensorActivity.this.startActivity(intent); } }); </code></pre> <hr> <p>EDIT: If you want that button class badly, you can try this:</p> <p>(Register the listeners using this "listener class" nested class inside your activity, or change it to public and put it on a different file and then register the listeners using this "listener class". Context will still be related to the MainActivity.)</p> <pre><code>private final class OnClickBtn implements OnClickListener { @Override public void onClick(final View view) { if( view.getId() == R.id.btnOpenA ){ final Context context = view.getContext(); final Intent intent = new Intent(context, ActivityA.class); view.getContext().startActivity(intent); } else if( view.getId() == R.id.btnOpenB ){ final Context context = view.getContext(); final Intent intent = new Intent(context, ActivityB.class); view.getContext().startActivity(intent); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload