matQ
matQ

Reputation: 617

EventBus in viewPager fragment issue

I have thee fragments in viewpager, Fragment1, Fragment2, and Fragment3.

fragment1 has a button and when onclick it setEnable(true) a button in fragment2 and the button in fragment 2 setEnable(true) a button3 in fragment3

All seem to work fine, but when I navigate through the fragments when passing from fragment 3 to fragment 1, fragment3 lose the information and the button comes back to its initial state -setEnable(false) as in the beginning, but fragment1 and fragment2 still work fine and don't lose the information when I navigate through all the fragments in viewpager.

How can I keep the information in fragment3 while navigating?

Here is my code:

fragment1

public class FragmentUno extends Fragment {
    Button btnClick;
    private EventBus bus = EventBus.getDefault();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_uno, container, false);
        btnClick = (Button)rootView.findViewById(R.id.button1);
        btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    bus.post(new EventoBotton());
                System.out.println("saliendoooo");
            }
        });
        return rootView;
    }
}

fragment2

public class FragmentDos extends Fragment {

    private EventBus bus = EventBus.getDefault();
    private EventBus bus2 = EventBus.getDefault();
    Button btnNext;

    @Override
    public void onResume() {
        super.onResume();
        bus.register(this);
    }
     @Override
    public void onPause() {
        super.onPause();
        bus.unregister(this);
    }
    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_dos, container, false);
        btnNext = (Button)rootView.findViewById(R.id.btnF2);
        btnNext.setEnabled(false);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //HERE I POST EVENT TO SEND TO FRAG 3
                bus2.post(new EventoDos());
            }
        });
        return rootView;
    }

    @Subscribe
    public void reenvio(EventoBotton ev) {
        btnNext.setEnabled(true);//HERE RECEIVE EVENT FORM FRAG 1
    }
}

Fragment3

public class FragmentTres extends Fragment {

    TextView txtDos;
    Button btnDis;
    private EventBus bus2 = EventBus.getDefault();

    @Override
    public void onResume() {
        super.onResume();
        bus2.register(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        bus2.unregister(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tres, container, false);
        txtDos = (TextView)rootView.findViewById(R.id.txtCambio);
        btnDis = (Button)rootView.findViewById(R.id.buttonF2);
        btnDis.setEnabled(false);

        return rootView;
    }

    @Subscribe
    public void actualizar(EventoDos eventoDos) {
        System.out.println("estamos aquiiii"); // Here I receive the event from frag 2
        txtDos.setText("hola mundo");
        btnDis.setEnabled(true);
    }
}

These are my POJO events

public class EventoBotton {

    public EventoBotton() {}
}

The other event

public class EventoDos {

    public EventoDos() {}
}

I'm using eventbus 3.0 with my SDK 21.

All works fine but when I slip through the view pager, if I slide from 3 to 2, the information is still there in frag3. But when I slide from 3 to 2 and reach 1 when I come back to 3, the information is lost. That is to say, the button in frag 3 come back to its original state (enabled false) Why?? How can I persist the changes in all the navigation in the viewpager?

Upvotes: 0

Views: 823

Answers (1)

codaddict
codaddict

Reputation: 454960

You can try using:

setOffscreenPageLimit(int limit) // In your case, limit = 2

on the ViewPager object which sets the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.

Upvotes: 1

Related Questions