Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I didn't feel that Commonsware's solution answered my question, because it required every container of the partial to add handlers for events that are contained wholly within the partial.</p> <p>I specifically do not want to have to implement handlers for every instance of that partial.</p> <p>So I came up with a solution of sorts, though I admit it doesn't feel right either.</p> <p>First, I subclass Activity, and create a small framework for associating a listener with startActivityForResult() and onActivityResult().</p> <pre><code>public class BaseActivity extends Activity { // assume that we'll never start more than one activity at a time from our activity (a safe assumption?) private static final int LISTENED_REQUEST_CODE = 1000000000; public static interface ActivityResultListener { public void onResultCode( int resultCode, Intent data ); } private ActivityResultListener activity_result_listener_; public void startActivityForResult( Intent intent, ActivityResultListener listener ) { // paranoia if (activity_result_listener_ != null) { Log.e( TAG, "Activity trying to start more than one activity at a time..." ); return; } activity_result_listener_ = listener; startActivityForResult( intent, LISTENED_REQUEST_CODE ); } public void onActivityResult( int requestCode, int resultCode, Intent data ) { if (requestCode == LISTENED_REQUEST_CODE) { if (activity_result_listener_ != null) { ActivityResultListener listener = activity_result_listener_; activity_result_listener_ = null; listener.onResultCode( resultCode, data ); return; } } super.onActivityResult(requestCode, resultCode, data); } } </code></pre> <p>Then inside the partial, I call my overloaded startActivityForResult() and implement a listener:</p> <pre><code>public void onFinishInflate() { ImageButton contact_button = (ImageButton)findViewById(R.id.contact_button); contact_button.setOnClickListener( new OnClickListener() { @Override public void onClick(View view) { ((BaseActivity)getContext()).startActivityForResult( new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI), new BaseActivity.ActivityResultListener() { @Override public void onResultCode( int resultCode, Intent data ) { if (resultCode == BaseActivity.RESULT_OK) { add_contact_address( data ); } } }); } } ); } </code></pre> <p>So now I can use this partial all over the place without having to define listeners for each instance.</p> <p>The drawback I see is that subclassing Activity will prevent me from using other Activity types. This could be reworked into an interface/implementation, but then starts to suffer from non-DRY logic once more.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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