2

So far, i was using Volley but after going through the Robospice library, and it's advantages over other network libraries, i started using it in my current project.

It working fine, but since it's lifecycle is tied to the activity not fragments, i implemented the same in fragment like this.

public class MainActivityFragment extends Fragment {
 private SpiceManager spiceManager = new SpiceManager(JacksonSpringAndroidSpiceService.class);
private static final String KEY_LAST_REQUEST_CACHE_KEY = "lastRequestCacheKey";
private String lastRequestCacheKey;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        performRequest(mSort);
        return rootView;
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        if (!TextUtils.isEmpty(lastRequestCacheKey)) {
            outState.putString(KEY_LAST_REQUEST_CACHE_KEY, lastRequestCacheKey);
        }
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null && savedInstanceState.containsKey(KEY_LAST_REQUEST_CACHE_KEY)) {
            lastRequestCacheKey = savedInstanceState.getString(KEY_LAST_REQUEST_CACHE_KEY);
            spiceManager.addListenerIfPending(MovieDataResult.class, lastRequestCacheKey,
                    new ListMovieRequestDataListener(movieImageAdapter, getActivity(),progressDialog));
            spiceManager.getFromCache(MovieDataResult.class, lastRequestCacheKey, DurationInMillis.ONE_MINUTE,
                    new ListMovieRequestDataListener(movieImageAdapter, getActivity(),progressDialog));

        }
        super.onActivityCreated(savedInstanceState);
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        spiceManager.start(getActivity());
    }

    @Override
    public void onStop() {
        super.onStop();
        if (spiceManager.isStarted()) {
            spiceManager.shouldStop();
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        mSort = Utility.getPreferredSorting(getActivity());
        setUpProgressDialog();
    }

    private void performRequest(String mSort) {
        MovieDataRequest movieDataRequest = new MovieDataRequest(apiKey,mSort);
        lastRequestCacheKey = movieDataRequest.createCacheKey();
        spiceManager.execute(movieDataRequest, lastRequestCacheKey,
                DurationInMillis.ONE_MINUTE, new ListMovieRequestDataListener(movieImageAdapter, getActivity(),progressDialog));
    }
} 

This works fine, i was wondering, if it follows the lifecycle. As, i am starting spicemanager onAttach() and stopping onStop() and onRestoreInstance() getting the cached data.

Basically, this is how it works here-->

  • spicemanager starts when fragment is attached
  • then .execute() is called for network request
  • spicemanager is destroyed onStop()

Also, is it a good approach to have a spicemanager for each activity?

Ritt
  • 3,181
  • 3
  • 22
  • 51

1 Answers1

1

I also pondered about this question recently as I was creating spiceManagers in both my activities and fragment classes.

This actually results in me having spiceManager objects in multiple places and for me, I do not think it was the most efficient way to do things.

Eventually, what I decided to do was to create a static instance of the spiceManager in my activity:

public static SpiceManager spiceManager = new SpiceManager(SpiceService.class);

Then use this static spiceManager within my fragments:

MainActivity.spiceManager.start(getActivity());

I also think that the most efficient way to do this is probably if you use interfaces to transfer data that you want handled by robospice back to the activity and just use the spiceManager inside your activity to do all the work. However, this can be a major piece of work.

Simon
  • 19,658
  • 27
  • 149
  • 217
  • Creating a static spicemanager and then using it in other activities sounds great, you won't have to create mutilple objects. But still you have to create different sepearte POJOs, SpiceRequest and Listeners for each activity, it depends on the api response. I didn't get your interface part, please elaborate more. – Ritt Oct 24 '15 at 20:51
  • Of course you will have to create different POJOs, SpiceRequest and Listeners as each network call is unique in any case. What I mean for the interfaces is that they are useful to pass data between different parts of android as they use the observer pattern to do so, like from adapter to activity, or from fragment to activity. See this question and answer that I posted about it: http://stackoverflow.com/questions/33131836/interface-observer-pattern-null-object-reference – Simon Oct 24 '15 at 20:54
  • Got it, it's basically creating an interface say infteraceA, which is implented in Activity A, and then the same implemented interface can be used in all other activites. This is more structured in terms of sharing data between activities. Thankx simon. – Ritt Oct 24 '15 at 21:02
  • Like I said, be prepared for a lot of rework in your app if you have already got an app on hand. If you are starting from scratch, it should be easier to implement these interfaces. If you do not want to do interfaces the old fashion way, there are libraries that you can employ, like ottobus for example. I do think that doing them the old fashion way is better though - it helps cuts down on the number of methods in your app and also the size of your app. – Simon Oct 24 '15 at 21:07
  • ottobus, never heard of this library.I will try it. I have started this app from scratch.Thankx for the library simon. – Ritt Oct 24 '15 at 21:14