1

Let's assume that we have Activity/Fragment which contains a RecyclerView. Furthermore, it sets an Adapter. For the sake of the example, let's say the Adapter has to have access to Fragment in order to call a method which displays a Snackbar. Moreover, Let's say there are a couple of items in the adapter. I want to delete one and remove it from the database. Therefore I should call ViewModel's methods. I've made a research but I couldn't find any information if referencing a fragment into the Adapter is good or not.

Could you help me and explain? Also for the ViewModel I've found some ideas here.

But what are the best practices?

radus14
  • 140
  • 2
  • 8
  • `let's say the Adapter has to have access to Fragment in order to call a method which displays a Snackbar.` that's the wrong way to implement it. the adapter knows nothing about the fragment, the adapter can invoke a callback to tell the fragment/activity to do something, but the adapter itself never tells the fragment directly to be doing something – a_local_nobody Mar 23 '22 at 10:19
  • @a_local_nobody but having a reference to the application context in adapter is fine, right? – radus14 Mar 23 '22 at 10:40
  • i would say it depends on what you're trying to achieve, i don't necessarily think activity context or even fragment context is always the worst, i suppose wherever possible it would be best to try avoid needing a reference to context in the first place, data can be passed from the fragment/activity perhaps – a_local_nobody Mar 23 '22 at 10:43

1 Answers1

1
  1. good Adapter Classes should be STATIC helping developers to keep it separated from Activity/Fragment part
  2. don't save Activity/Fragment reference inside Adapters
  3. ViewModels should belongs to Activities or Fragments
  4. Adapters should execute Activity/Fragment's actions via Callbacks/Listeners or LiveData

Pseudo-code:

public class MainActivity extends Activity {
    
    private interface Listener {
        void OnRemoved(@NonNull xxx removedItem);
    }
    
    private static final class MyAdapter extends ArrayAdapter<xxx> {
        private final Listener mListener;
        private MyAdapter(@NonNull final Listener listener) {
            super(...);
            this.mListener = listener;
        }
        @Override
        public void remove(xxx item) {
            super.remove(xxx); //<-- this removes item from Adapter
            this.mListener.OnRemoved(item); //<-- this triggers Activity's code
        }
    }
    
    public void onCreate(...) {
        ...
        new MyAdapter(new Listener() {
            @Override
            public void OnRemoved(@NonNull final xxx removedItem) {
                Snakbar.makeText(....).show();
            }
        });
    }
}
emandt
  • 2,547
  • 2
  • 16
  • 20
  • `good Adapters should be STATIC` really ? i've never seen or used a static adapter, do you have any resources for this ? – a_local_nobody Mar 23 '22 at 10:26
  • I mean (already fixed) "Adapter Classes should be STATIC". In this way it's easier to separate Adapter's logic from Activity/Fragment's part because Activity/Fragment's methods/components cannot be directly used from the Adapter even if the Adapter is declared (non-static) inside an Activity/Fragment. – emandt Mar 23 '22 at 10:34
  • yeah i suppose that makes sense to an extent, i agree that adapters should be reusable components which don't rely on any particular fragment or activity, i think that's what we're both trying to say – a_local_nobody Mar 23 '22 at 10:39
  • @emandt then what should be the flow of the logic if I want to delete an item from the adapter and show a snackbar in fragment? How do I announce the ViewModel for the deletion? What and How is the order? Callback to Fragment which calls VM? Or somehow call VM and then Fragment observes it? – radus14 Mar 23 '22 at 10:39
  • `Callback to Fragment which calls VM?` yes, like this – a_local_nobody Mar 23 '22 at 10:50
  • ^emandt & @a_local_nobody Thank you guys! Also, ^a_local_nobody I was wondering if you have any official reference for these? – radus14 Mar 23 '22 at 12:08