Search code examples
androidandroid-fragmentsandroid-viewpager

How to call a method of a InfiniteViewPager fragment from activity


I am trying to call some of the method of my fragment ,But the thing is I am using Infinite ViewPager not normal ViewPager .I tried the following way to call the fragment method.

  1. I cant set the id as it is in a Infinite ViewPager fragment.
  2. I am not able to use the fragment tag in java .
  3. I tried the getItem method but it result in a crash .error

FATAL EXCEPTION: main Process: beatbox.neelay.dialogtest2, PID: 28997 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference at beatbox.neelay.dialogtest2.Songdata.getPlayList(Songdata.java:26) at beatbox.neelay.dialogtest2.Songs.dataretrive(Songs.java:56) at beatbox.neelay.dialogtest2.MainActivity.onFragmentInteraction(MainActivity.java:134) at beatbox.neelay.dialogtest2.BlankFragment.onButtonPressed(BlankFragment.java:70) at beatbox.neelay.dialogtest2.BlankFragment$2.onClick(BlankFragment.java:60) at android.view.View.performClick(View.java:5207) at android.view.View$PerformClick.run(View.java:21177) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5441) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

One more problem in InfiniteViewPager is the position is not the same if the user keep on swiping its like a spiral . this is the way i called the InfiniteViewPager

private InfiniteViewPager pager = null;
private MyAdapter adapter;
private InfinitePagerAdapter myAdapter;

 pager = (InfiniteViewPager) findViewById(R.id.pager);
    FragmentManager fragmentManager = getSupportFragmentManager();


     adapter = new MyAdapter(fragmentManager);
     myAdapter = new InfinitePagerAdapter(adapter);
    pager.setAdapter(myAdapter);

and I tried this way to call the method

   int pos = pager.getCurrentItem();
    Fragment activeFragment = adapter.getItem(pos);
    if(pos == 2){
        ((Songs)activeFragment).dataretrive();
        ((Songs)activeFragment).dataset();

    }

any hint how to call the method will be helpful.


Solution

  • Usually, it is bad practice to call directly methods of the fragment from the activity.

    I would recommend you to do this ussing notify/listeners mechanism. Something like these:

    public interface SomeEventListener {
        void onEvent();
    }
    
    public interface SomeEventNotifier {
        void addListener(SomeEventListener listener);
    
        void removeListener(SomeEventListener listener);
    }
    
    public class ExampleActivity extends Activity implements SomeEventNotifier {
        private final List<SomeEventListener> listeners = new CopyOnWriteArrayList<>();
    
        @Override
        public void addListener(SomeEventListener listener) {
            listeners.add(listener);
        }
    
        @Override
        public void removeListener(SomeEventListener listener) {
            listeners.remove(listener);
        }
    
        private void notifySomeEvent() {
            for (SomeEventListener listener : listeners) {
                listener.onEvent();
            }
        }
    }
    
    public class ExamplpeFragment extends Fragment implements SomeEventListener {
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            if (isIntresterInSomeEvent()) {
                SomeEventNotifier notifier = (SomeEventNotifier) getActivity();
                notifier.addListener(this);
            }
        }
    
        @Override
        public void onDetach() {
            super.onDetach();
            if (isIntresterInSomeEvent()) {
                SomeEventNotifier notifier = (SomeEventNotifier) getActivity();
                notifier.removeListener(this);
            }
        }
    
        private boolean isIntresterInSomeEvent() {
            //here you have to add you logic to determinate rather the instance of fragment need notification about the event
            return true;
        }
    
        @Override
        public void onEvent() {
            //here you will get call when activity notifySomeEvent is called.
        }
    }
    

    All attached fragments will be notified when notifySomeEvent is called.