FragmentStatePageAdapter保留连接到片段的接口

时间:2016-09-03 14:15:04

标签: java android android-fragments android-viewpager fragmentstatepageradapter

我正在使用FragmentStatePagerAdapter(对于ViewPager),附加到ViewPager的片段包含我希望将数据传递回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);
    }
}

1 个答案:

答案 0 :(得分:1)

我认为您应该阅读此https://developer.android.com/training/basics/fragments/communicating.html

让活动实现接口并使用onAttach()onDetach()方法重置对活动的回调引用。

如果您尝试保留片段,请使用您的代码。 state(setRetainInstance(true),onsaveInstanceState())包含可能存在内存泄漏问题的接口对象,或者代码将无缘无故变得太复杂。