patternjavaMinor
Where to define constants (Bundle Keys) used between 2 Activities?
Viewed 0 times
bundleusedwhereactivitieskeysbetweendefineconstants
Problem
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
Solution
One thing that I find is that it is not necessary to wrap your keys inside their own inner class. Putting them inside
Becomes:
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
I do feel that it is more common to do your first version: (but without using the
One thing I really have to point out is that this method is a bit overkill:
I would prefer to have the parent activity call
To summarize: (My opinions at least)
In the end, which way you choose is up to you.
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.startActivitystartActivity(SecondActivity.prepareIntent(userName, userPhoto));To summarize: (My opinions at least)
- Drop the
startstatic method entirely
- Drop the
Keyinner 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.
Code Snippets
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";
}
...public static final String USER_NAME = "USER_NAME";
public static final String USER_PHOTO = "USER_PHOTO";private void onOkButtonPressed() {
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(SecondActivity.USER_NAME, userName);
intent.putExtra(SecondActivity.USER_PHOTO, userPhoto);
startActivity(intent);
}public static void start(Context context, String userName, Uri userPhoto) {
Intent intent = prepareIntent(context, userName, userPhoto);
context.startActivity(intent);
}startActivity(SecondActivity.prepareIntent(userName, userPhoto));Context
StackExchange Code Review Q#56949, answer score: 3
Revisions (0)
No revisions yet.