0

I'm following this tutorial from Android Developers site http://developer.android.com/training/animation/screen-slide.html

My situation is this...

I have N fragments that represent every page in my viewpager, every fragment is different and has its own layout. That's why I have different classes for each type of fragment.

I want to obtain the value of different edittext I have in the first pages but in the last one I want to process that information.

I have no idea how to solve this

user3626174
  • 3
  • 1
  • 2
  • try to use interface like [this](http://developer.android.com/training/basics/fragments/communicating.html) – calvinfly Feb 24 '15 at 02:00

3 Answers3

0

You can use Intent or Bundle to pass information from one Fragment to other Fragments. Process that information in 2nd Fragment and again use Intent or Bundle to pass processed result to first fragment.
Example: Simple example for Intent and Bundle

Community
  • 1
  • 1
KulArtist
  • 965
  • 8
  • 20
0

As mentioned by @calvinfly, try implementing an interface. Because each individual fragment is unique and does not know about each other, the only link between them is the adapter that created them. Thus, you could set a callback between the Fragment and its Adapter:

public class DummyFragment extends Fragment {
    private OnEditTextSendListener mListener;
    ...
    public interface OnSendTextListener { // this requires the adapter to implement sendMessage()
        public void sendMessage(CharSequence msg);
    }

    public void setOnSendTextListener(OnSendTextListener listener) {
        mListener = listener;
    }

    public View onCreateView( ... ) {
        ...
        (EditText) editText = (EditText) rootView.findViewById(R.id.editText);
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                mListener.sendMessage(v.getText());
            }
        }
    }
}

Notes: Specify listening to a particular actionId if desired (such as EditorInfo.IME_ACTION_SEND.

Then in your Pager Adapter:

public class SectionsPagerAdapter extends FragmentStatePagerAdapter 
    implements DummyFragment.OnSendTextListener {
    private String mReceivedMessage;

    // the required method to implement from the interface
    public void sendMessage(CharSequence msg) {
        mReceivedMessage = msg.toString();
        notifyOnDataSetChanged(); // tell the adapter views need to be updated
    }
}

From here, your adapter now receives your EditText inputs when you perform an action on your EditText (once the Adapter calls setOnSendTextListener and sets it to itself, which you'll see below). The only thing left is to pass this message back to the corresponding fragment, probably during getItem as one of the arguments in a Bundle.

public Fragment getItem(int position) {
    Fragment fragment;
    Bundle args = new Bundle();
    switch (position) {
        case 0:
            fragment = new DummyFragment();
            ((DummyFragment) fragment).setOnSendTextListener(this);
            break;
        case 1:
            // your other fragment
        case 2:
            // your other fragment that wants the message from 1st fragment
            args.putString(DummyOtherFragment.ARG_DUMMYFRAGMENT_MSG, mReceivedMessage);
            break;
        default:
    }

    // other arguments here
    fragment.setArguments(args);
    return fragment;
}

Hope this helps - see the Android developers guide Creating event callbacks to the activity) for more info.

Side note: You will probably encounter the issue where your Adapter correctly receives the callback info, but the fragment it is passing the data to doesn't reload with the data (even though you're calling notifyOnDataSetChanged(). That is a whole other issue on its own and I'd like to direct you to another SO question about ViewPager refreshing fragments for further reading.

Community
  • 1
  • 1
ekchang
  • 939
  • 5
  • 11
0

In the tutorial you posted, I think it is clear that you have to implement the getItem override method. Look for text in that webpage:

Creates a class that extends the FragmentStatePagerAdapter abstract class and implements the getItem()

The idea of extending a Fragment or PagerAdapter is common. I have done it for PagerAdapter and I was pleased.

The Original Android
  • 6,147
  • 3
  • 26
  • 31