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.
1 Answer 1
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 intoSecondActivity
- 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.
-
\$\begingroup\$ Instead of
public static final
shouldn't we useprivate static final
bcoz those keys are only used inside SecondActivity.class? \$\endgroup\$Android Developer– Android Developer2020年08月23日 11:18:38 +00:00Commented Aug 23, 2020 at 11:18 -
\$\begingroup\$ @AndroidDeveloper For sure, yes. \$\endgroup\$Simon Forsberg– Simon Forsberg2020年08月25日 20:55:36 +00:00Commented Aug 25, 2020 at 20:55