How to pass onClick function to Intent
<p>I want to modularize the usage of my class but I have problem in passing function. I want to be able to pass an <strong>OnClickListener</strong> from 1 activity to this <strong>CoachmarkActivity</strong>.</p>

<p>I tried 2 different method:
 1. Passing an OnClickListener to Intent
 2. Passing a class, FollowUpClass, implements Serializable, which has method onClick.</p>

<p>You can see the code below. It is not complete code, but you should be able to comprehend this.</p>

<pre><code>public class CoachmarkActivity extends Activity {

    public static final String RES_LAYOUT = "RES-LAYOUT";
    public static final String LISTENER = "LISTENER";
    public static final String FOLLOW_UP = "FOLLOW-UP";

    @Override protected void onCreate(Bundle savedInstance) {

        setContentView(getIntent.getIntExtra(RES_LAYOUT, R.layout.activity_default))

        Button button1 = (Button) findViewById(R.id.button1);
        Button button2 = (Button) findViewById(R.id.button2);

        // 1ST ATTEMPT        
        // I want to modularize this
        OnClickListener onClickPassedFromIntent = (OnClickListener) getIntent().getSerializableExtra(LISTENER);
        button1.setOnClickListener(onClickPassedFromIntent);

        // 2ND ATTEMPT
        final FollowUpListener folllowup = (FollowUpListener) getIntent().getSerializableExtra(FOLLOW_UP);
        button2.setOnClickListener(new OnClickListener() {
            @Override void onClick() {
                // !! Here is error, exception thrown
                folllowup.onClick();
            }
        });
    }

    /**
     * Public method to be used in other activity.
     * Invocation wanna be:
     *   CoachmarkActivity.startThisActivity(getActivity(), R.layout.coachmark1, new OnClickListener() {
     *      @Override void onClick() {
     *          // Do something
     *      }
     *   });
     */
    public static void startThisActivity(Context context, int resId, OnClickListener listener) {
        Intent intent = new Intent(context, CoachmarkActivity.class);
        intent.putExtra(RES_LAYOUT, resId);
        // !! Line below is error, onClickListener is not serializable, no method can accomadate below
        intent.putExtra(LISTENER, listener);
        context.startActivity(intent);
    }

    /**
     * Public method to be used in other activity.
     * Invocation wanna be:
     *   CoachmarkActivity.startThisActivity(getActivity(), R.layout.coachmark1, new FollowUpListener() {
     *      @Override void onClick() {
     *          // Do something
     *      }
     *   });
     */
    public static void startThisActivity(Context context, int resId, FollowUpListener folllowup) {
        Intent intent = new Intent(context, CoachmarkActivity.class);
        intent.putExtra(RES_LAYOUT, resId);
        intent.putExtra(FOLLOW_UP, followup);
        context.startActivity(intent);
    }
}
</code></pre>

<p>The abstract class: </p>

<pre><code>public abstract class FollowUpListener implements Serializable {
    public abstract void onClick();
}
</code></pre>

<p>The problems are stated in the comment in source code above, with tag "!!" (Just CTRL+F "!!"). What I want to do is like passing a Delegate object (function in form of variable) in C#, but in Android Java.</p>

<p>Any idea? Thanks.</p>