1

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?

Vinny K
  • 194
  • 15
  • Not sure what you mean. Having your constant public doesn't change. When you put the extra in, don't you need the name to get it out? Also, this is like asking, can I make a method that does some stuff? Yes, you can. Is it needed or necessary, depends. I disagree with the ease of refactoring. You've basically written a method that is highly specialized. How many overrides do you want to write. I'd probably add the class as a param, you could then start various new activities. – ChiefTwoPencils Jun 16 '14 at 23:30
  • @ChiefTwoPencils Having a need for public constants do change. Instead of needing to know the String key for the Extra, I can just pass it to a constructor when creating an intent. So the value of that Key only needs to be known by the destination activity. So it can be private. I know I _can_ do it. I'm asking if it's good practice, common usage and if there any downfalls to it. Shouldn't methods be highly specialized? The method has one job, create an intent for it's containing class with the extras passed in to it. It's not an override, so I'm not sure what you mean? – Vinny K Jun 17 '14 at 01:03
  • Today I asked to myself the same question! :D – Diego Palomar Aug 04 '14 at 21:31

2 Answers2

1

I've wondered the same thing, and I conclude that this is most likely because activities are meant to be somewhat static; that they're only meant as a container for fragments (which are meant to be changing), regarding the presentation layer, of course, and as an interface (kind of) between another activity and the fragment they host. I realized that on my question: Is there a convention for creating, updating and passing data to Fragments or Activities in Android?

Something like this:

A fragment (on modern UIs) is the ultimate part of the UI which holds the displayed data (though there's no rule for this, it's just a common practice).

But in the end, there is no told rule for how you should create your activities, it's just that since fragments are most commonly the ones who change based on the data is passed between activites, it's easier to focus on methods for buidling fragments rather than activities.

Community
  • 1
  • 1
arielnmz
  • 8,354
  • 9
  • 38
  • 66
0

The AndroidAnnotations framework generates exactly that Intent Builder for annotated classes. Look at this example extras annotation and the builder pattern invocation below.

Bananeweizen
  • 21,797
  • 8
  • 68
  • 88