7
\$\begingroup\$
public class FirstActivity extends Activity {

    private String userName;
    private Uri userPhoto;

    private void onOkButtonPressed() {
        Intent intent = new Intent(this, SecondActivity.class);
        intent.putExtra(SecondActivity.Key.USER_NAME, userName);
        intent.putExtra(SecondActivity.Key.USER_PHOTO, userPhoto);
        startActivity(intent);
    }

}


public class SecondActivity extends Activity {

    public static class Key {
        public static final String USER_NAME = "USER_NAME";
        public static final String USER_PHOTO = "USER_PHOTO";
    }

    private String userName;
    private Uri userPhoto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.container);
        init();
    }

    private void init() {
        Intent intent = getIntent();
        userName = intent.getStringExtra(Key.USER_NAME);
        userPhoto = intent.getParcelableExtra(Key.USER_PHOTO);
    }

}

Or maybe it would be better to make the keys private inside SecondActivity and do something like this?

public class SecondActivity extends Activity {

    private static class Key {
        public static final String USER_NAME = "USER_NAME";
        public static final String USER_PHOTO = "USER_PHOTO";
    }

    public static void start(Context context, String userName, Uri userPhoto) {
        Intent intent = prepareIntent(context, userName, userPhoto);
        context.startActivity(intent);
    }

    public static Intent prepareIntent(Context context, String userName, Uri userPhoto) {
        Intent intent = new Intent(context, SecondActivity.class);
        intent.putExtra(Key.USER_NAME, userName);
        intent.putExtra(Key.USER_PHOTO, userPhoto);
        return intent;
    }

    // skipped

}

It slightly improves readability:

SecondActivity.start(this, userName, userPhoto);

But on the other hand, it becomes complicated to make changes. Every time when I want add new fields or change existing fields, I should also change the arguments list in the method start(). And this way is also not suitable in the case when number of the input parameters will increase significantly.

\$\endgroup\$
3
\$\begingroup\$

One thing that I find is that it is not necessary to wrap your keys inside their own inner class. Putting them inside SecondActivity directly is enough.

public class SecondActivity extends Activity {
    public static class Key {
        public static final String USER_NAME = "USER_NAME";
        public static final String USER_PHOTO = "USER_PHOTO";
    }
    ...

Becomes:

public static final String USER_NAME = "USER_NAME";
public static final String USER_PHOTO = "USER_PHOTO";

Every time when I want add new fields or change existing fields, I should also change the arguments list in the method start(). And this way is also not suitable in the case when number of the input parameters will increase significantly.

You are correct about that.

Even though I have to say that I do like your idea of a prepareIntent method.

I do feel that it is more common to do your first version: (but without using the Key inner class).

private void onOkButtonPressed() {
    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra(SecondActivity.USER_NAME, userName);
    intent.putExtra(SecondActivity.USER_PHOTO, userPhoto);
    startActivity(intent);
}

One thing I really have to point out is that this method is a bit overkill:

public static void start(Context context, String userName, Uri userPhoto) {
    Intent intent = prepareIntent(context, userName, userPhoto);
    context.startActivity(intent);
}

I would prefer to have the parent activity call prepareIntent, which gives you the possibility of changing or adding some of the extras passed to the intent. I also find it better because you can in a parent activity call startActivity directly without the need for context.startActivity

startActivity(SecondActivity.prepareIntent(userName, userPhoto));

To summarize: (My opinions at least)

  • Drop the start static method entirely
  • Drop the Key inner class and put the constants directly into SecondActivity
  • It seems to be more common to create the intent inside the parent activity

In the end, which way you choose is up to you.

\$\endgroup\$
2
  • \$\begingroup\$ Instead of public static final shouldn't we use private static final bcoz those keys are only used inside SecondActivity.class? \$\endgroup\$ Aug 23 '20 at 11:18
  • \$\begingroup\$ @AndroidDeveloper For sure, yes. \$\endgroup\$ Aug 25 '20 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.