Note that there are some explanatory texts on larger screens.

plurals
  1. POBest practice for defining button events in android
    text
    copied!<p>I have a <code>Layout</code> defined in XML which consists of several <code>Button</code>s. </p> <p>Currently I am doing this in the <code>OnCreate</code> method to define the event handlers against the buttons:</p> <pre><code>public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button newPicButton = (Button)findViewById(R.id.new_button); newPicButton.setOnClickListener(btnListener); ..... similarly for other buttons too ..... } </code></pre> <p>Inside of the <code>Button</code>'s <code>onClick</code> event, I launch a camera <code>Intent</code> to get a picture and inside the <code>onActivityResult</code> callback I am again setting the event handlers along with setting the <code>View</code> like this:</p> <pre><code>protected void onActivityResult(int requestCode, int resultCode, Intent data) { setContentView(R.layout.main); Button newPicButton = (Button)findViewById(R.id.new_button); newPicButton.setOnClickListener(btnListener); ...similarly for other buttons too } </code></pre> <p>I am new to android and this approach of redefining an event every time seems quite dirty to me. I would like to know what is the best practice in terms of defining button event handlers in scenarios like this.</p> <p>Edit: pasting my complete class</p> <pre><code>public class CameraAppActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button newPicButton = (Button)findViewById(R.id.new_button); newPicButton.setOnClickListener(btnListener); } //---create an anonymous class to act as a button click listener--- private OnClickListener btnListener = new OnClickListener() { public void onClick(View v) { //Intent newPicIntent = new Intent(v.getContext(), NewPictureActivity.class); //startActivityForResult(newPicIntent, 0); Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, 999); } }; protected void onActivityResult(int requestCode, int resultCode, Intent data) { setContentView(R.layout.main); Button newPicButton = (Button)findViewById(R.id.new_button); newPicButton.setOnClickListener(btnListener); //if I comment last two lines nothing happens when I click on button } </code></pre> <p><strong>The main question is</strong></p> <pre><code>setContentView(R.layout.main); Button newPicButton = (Button)findViewById(R.id.new_button); newPicButton.setOnClickListener(btnListener); </code></pre> <p>Re-registering events inside <code>onActivityResult</code>.. is it right approach? Or am I doing something wrong? Because If I don't re-register event nothing happens when I click the button.</p>
 

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