FragmentStatePageAdapter 保留接口附加到片段

FragmentStatePageAdapter Retaining interface attached to fragment

我正在使用 FragmentStatePagerAdapter(对于 ViewPager),附加到 ViewPager 的片段具有我希望将数据传回 Activity。我的问题在于,一旦设备旋转,我传递给片段的界面对象就会丢失。

我的问题是如何保留界面对象。
现在是强制性代码片段:

Activity:

public class TheActivity extends AppCompatActivity {

    static final int PageCount = 2;
    static final int PageA = 0;
    static final int PageB = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {

                switch (position) {
                    case PageA: {
                        PageAFragment fragment = PageAFragment.CreateFragment();
                        fragment.setDataListener(new PageAFragment.DataListener() {
                            @Override
                            public void OnPageADataChange(String value) {
                                PageAData = value;
                            }
                        });
                        return fragment;
                    }
                    case PageB: {
                        PageBFragment fragment = PageBFragment.CreateFragment(2);
                        fragment.setDataListener(new PageBFragment.DataListener() {
                            @Override
                            public void OnPageAOneDataChange(String value) {
                                PageBDataOne = value;
                            }

                            @Override
                            public void OnPageATwoDataChange(String value) {
                                PageBDataTwo = value;
                            }
                        });
                        return fragment;
                    }
                }

                return null;
            }

            @Override
            public int getCount() {
                return PageCount;
            }
        });
    }
}

碎片:

public class PageAFragment extends Fragment {

    private DataListener mListener;

    public static PageAFragment CreateFragment() {
        return new PageAFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.page_a, container, false);
        EditText editText = (EditText) view.findViewById(R.id.edittext_data_a);
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionID, KeyEvent event) {

                if (actionID == EditorInfo.IME_ACTION_DONE)
                    mListener.OnPageADataChange(editText.getText().toString());
                return false;
            }
        });
        return view;
    }

    public void setDataListener(DataListener dataListener) {
        mListener = dataListener;
    }

    public interface DataListener {
        void OnPageADataChange(String value);
    }
}


public class PageBFragment extends Fragment {

    private DataListener mListener;

    public static PageBFragment CreateFragment(int data) {
        PageBFragment fragment = new PageBFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("Data", data);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.page_b, container, false);
        EditText editTextOne = (EditText) view.findViewById(R.id.edittext_data_b_one);
        editTextOne.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionID, KeyEvent event) {
                if (actionID == EditorInfo.IME_ACTION_DONE)
                    mListener.OnPageAOneDataChange(editTextOne.getText().toString());
                return false;
            }
        });
        EditText editTextTwo = (EditText) view.findViewById(R.id.edittext_data_b_two);
        editTextTwo.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionID, KeyEvent event) {
                if (actionID == EditorInfo.IME_ACTION_DONE)
                    mListener.OnPageATwoDataChange(editTextTwo.getText().toString());
                return false;
            }
        });
        return view;
    }

    public void setDataListener(DataListener dataListener) {
        mListener = dataListener;
    }

    public interface DataListener {
        void OnPageAOneDataChange(String value);

        void OnPageATwoDataChange(String value);
    }
}

我认为你应该重新阅读这篇文章 https://developer.android.com/training/basics/fragments/communicating.html

让 activity 实现接口并使用 onAttach() onDetach() 方法将回调的引用重置为 activity。

使用您的代码,如果您尝试保留包含接口对象的片段状态 (setRetainInstance(true), onsaveInstanceState()),您可能会遇到内存泄漏问题,或者您的代码会无缘无故地变得太复杂.