Pass a list of complex objects from activity to fragment

I have a MainActivity with SearchFragment and SearchResultFragment.

When the user clicks on the search button, I am making a callback from SearchFragment to MainActivity with SearchParameters as parameters.

In MainActivity I show LoadFragment and load the results.
When I receive the results, I want to pass a list of result objects to the SearchResultFragment.

What's the best way to do this?

I know you usually do it like this if it's only one result:

public static SearchResultFragment newInstance(SearchResult result) {
    SearchResultFragment f = new SearchResultFragment();
    Bundle args = new Bundle();
    args.putInt("result", result);  // Make result parceable
    f.setArguments(args);
    return f;
} 

      

But how do you do this with a list of objects?

Another solution I can think of is getting a reference to MainActivity in the onAttach method of the fragment, and then calling the getResults () method in MainActivity.

What's the best way?

+3


source to share


2 answers


Most of the time you can avoid having to pass complex objects, but if you can't, just use getters and setters. Try something like this:

public static SearchResultFragment newInstance(SearchResult result) {
    SearchResultFragment f = new SearchResultFragment();
    f.setResult(result);        
    return f;
}

      



BUT try to avoid this, transferring data using Bundle

is a much better solution. Use Parcelable

wherever you can.

+6


source


Note. My answer assumes that you are trying to pass the data aggregation to the chunk once. If you are trying to regularly update an existing snippet with new information (new results), then it might be better to have a dedicated method in Fragment

to save the results as arguments and update the UI.

Both approaches may be acceptable, especially depending on the scope you want to be in the search results, but I would prefer to pass the results as a "static" parameter (which means it is not dynamically picked from Activity

), mainly for the following reasons:

  • Fragment

    are intended to be used in reusable parts of the user interface, which means that the Android platform allows one and the same Fragment

    to be integrated into another Activity

    ; in this case it is easier to integrate (more flexible) than if the attached one Activity

    should implement the given getter, but Fragment

    could be constructed by another object (for example, a "result receiver") and attached to Activity

    , not knowing about the results (facilitates potential refactoring) Arguments
  • Fragment

    persist, which means that the same initialization code Fragment

    (which retrieves the results from the arguments) will also handle auto-reinstall Fragment

    after restarting the activity (like screen rotation); otherwise, you will additionally have to handle saving and restoring the result object yourself.

On the other hand, adding the results to the Bundle

as parameter Fragment

will be a little slower, since after getting new results, you create an additional structure ( Bundle

) that you then send (serialize) and don't receive (Deserialize). It will be faster on restart Activity

, although in this case you do not need to unload it (this is already done). I just mentioned it for the sake of completeness, but since then, if your results are not very large (this is a valid use case, but perhaps more suitable for page loading?), You won't notice and shouldn't care anyway.



To pass multiple results to Fragment

(assuming one result is equal int

):

final Bundle args = new Bundle();
args.putIntArray(SearchResultFragment.ARG_NAME_RESULTS, results);  // results being an int[]
fragment.setArguments(args);

      

Just use Bundle#putIntegerArrayList(String, ArrayList<Integer>)

if it results

has a type ArrayList<Integer>

.

+4


source







All Articles