In some fragment examples I've seen, there are static methods to get an instance of a fragment out of a class with parameters like extras without passing variables. This example comes from the developer.android.com site.
public static DetailsFragment newInstance(int index) {
DetailsFragment f = new DetailsFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
In the past, when passing extras to new a new activity I've always done something like the following.
Intent newActivity = new Intent(this, NewActivity.class);
newActivity.putExtra(NewActivity.extra_1, extra_value);
startActivity(newActivity);
Where NewActivity.extra_1 is a public constant for the value.
Is there any reason not to do the same builder pattern I've seen in fragments? It's nothing I've ever seen or can find an answer on. Why not create something like:
public static Intent instance(Context c, String extra1, long extra2){
Intent i = new Intent(c, NewActivity.class);
i.putExtra(EXTRA_1, extra1);
i.putExtra(EXTRA_2, extra2);
return i;
}
And then the call becomes:
startActivity(NewActivity.instance(this, extra1, extra2);
The extra constants then don't need to be public, if a new extra is required for the Activity, the instance method can be changed for ease of refactor and I think it cleans up the startActivity call.
Am I just late to the game on this?