Access Fragment in Activity?

前端 未结 4 1609
有刺的猬
有刺的猬 2020-12-03 19:37

I have one main activity with 2 fragments. Both fragments have a ListView. I want to update the list in MainActivity. Is there any way to access fr

4条回答
  •  盖世英雄少女心
    2020-12-03 20:32

    You could do the following with Otto event bus:

    public class UpdateListEvent {
        private int fragmentState;
    
        public UpdateListEvent(int fragmentState) {
            this.fragmentState = fragmentState;
        }
    }
    
    public class MainActivity extends ActionBarActivity {
        ...
        public void updatelist() {
           SingletonBus.INSTANCE.getBus().post(new UpdateListEvent(fragmentState));
        }
    }
    
    public class FragmentA extends Fragment {
        @Override
        public void onResume() {
            super.onResume();
            SingletonBus.INSTANCE.getBus().register(this);
        }
    
        @Override
        public void onPause() {
            SingletonBus.INSTANCE.getBus().unregister(this);
            super.onPause();
        }
    
        @Subscribe
        public void onUpdateListEvent(UpdateListEvent e) {
            if(e.getFragmentState() == 0) { //is this even necessary?
                this.adapter.notifyDataSetChanged();
            }
        }
    }
    
    public class FragmentB extends Fragment {
        @Override
        public void onResume() {
            super.onResume();
            SingletonBus.INSTANCE.getBus().register(this);
        }
    
        @Override
        public void onPause() {
            SingletonBus.INSTANCE.getBus().unregister(this);
            super.onPause();
        }
    
        @Subscribe
        public void onUpdateListEvent(UpdateListEvent e) {
            if(e.getFragmentState() != 0) { //is this even necessary?
                 this.adapter.notifyDataSetChanged();
            }
        }
    }
    

    And a revised version of the Singleton Bus

    public enum SingletonBus {
        INSTANCE;
    
        private static String TAG = SingletonBus.class.getSimpleName();
    
        private Bus bus;
    
        private volatile boolean paused;
    
        private final Vector eventQueueBuffer = new Vector<>();
    
        private Handler handler = new Handler(Looper.getMainLooper());
    
        private SingletonBus() {
            this.bus = new Bus(ThreadEnforcer.ANY);
        }
    
        public  void postToSameThread(final T event) {
            bus.post(event);
        }
    
        public  void postToMainThread(final T event) {
            try {
                if(paused) {
                    eventQueueBuffer.add(event);
                } else {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                bus.post(event);
                            } catch(Exception e) {
                                Log.e(TAG, "POST TO MAIN THREAD: BUS LEVEL");
                                throw e;
                            }
                        }
                    });
                }
            } catch(Exception e) {
                Log.e(TAG, "POST TO MAIN THREAD: HANDLER LEVEL");
                throw e;
            }
        }
    
        public  void register(T subscriber) {
            bus.register(subscriber);
        }
    
        public  void unregister(T subscriber) {
            bus.unregister(subscriber);
        }
    
        public boolean isPaused() {
            return paused;
        }
    
        public void setPaused(boolean paused) {
            this.paused = paused;
            if(!paused) {
                Iterator eventIterator = eventQueueBuffer.iterator();
                while(eventIterator.hasNext()) {
                    Object event = eventIterator.next();
                    postToMainThread(event);
                    eventIterator.remove();
                }
            }
        }
    }
    
        

    提交回复
    热议问题