在 viewpager 中保存和恢复片段状态

Saving and restoring fragment state in viewpager

这可能是一个重复的问题,但似乎 none 提供的答案可以解决我的问题。所以我有一个 class 和 extends ViewPager 并且在水平导航中,我有 3 个页面。在 FragmentonActivityCreated() 方法之一中,我正在调用服务器,获取一些数据并在 TableLayout 中插入 TableRows。现在我想保存这个确切的状态并在导航回相同的 Fragment.

时恢复它

我注意到一件奇怪的事情是,当我从一个 Tab 滑动到下一个然后再回到第一个时,第一个 Tab 的状态没有变化。然而,在从第一导航到第二导航到第三导航然后回到第一导航时,我失去了状态。除了使用 onSaveInstanceState() 之外,是否有任何方法可以保存 Fragment 的状态,然后在以后恢复它?因为这需要我手动将所有数据存储在 Bundle 中。这是我的 Fragment

public static class ProfileFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

        View fragmentView = inflater.inflate(R.layout.fragment_profile, container, false);
        return fragmentView;
    }

    private final int EMPLOYER_TABLE_ROWS = 3;
    private final int STUDENT_TABLE_ROWS = 7;
    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        HasuraClient client = Hasura.getClient();

        // make network calls to fetch data from server

        HasuraUser user = client.getUser();
        String[] role = user.getRoles();

        if(role[1].equals(getString(R.string.student_role).toLowerCase())){
            StudentProfileQuery query = new StudentProfileQuery(user.getId());
            client.asRole(getString(R.string.student_role).toLowerCase()).useDataService()
                    .setRequestBody(query)
                    .expectResponseType(ProfileResponse.class)
                    .enqueue(new Callback<ProfileResponse, HasuraException>() {
                        @Override
                        public void onSuccess(ProfileResponse response) {
                            // Handle response

                            // get headingContainer
                            LinearLayout headingContainer = (LinearLayout) getView().findViewById(R.id.name_and_institute);
                            // get inflater object
                            LayoutInflater inflater = LayoutInflater.from(getContext());

                            // place name
                            TextView headingName = (TextView) inflater.inflate(R.layout.profile_page_title, headingContainer, false);
                            headingName.setText(response.getName());
                            headingContainer.addView(headingName, 0);

                            // place instituteName
                            TextView headingInstitution = (TextView) inflater.inflate(R.layout.profile_page_title, headingContainer, false);
                            headingInstitution.setText("Student" + " at " + response.getInstitution());
                            headingContainer.addView(headingInstitution, 1);

                            String[] field = {"Email", "Date Of Birth", "Gender", "Year of Admission", "Year of Passing", "Percentage",
                                    "Resume URL"};
                            String[] detail = {response.getEmail(), response.getDob(), response.getGender(),
                                    response.getYear_of_admission().toString(), response.getYear_of_passing().toString(),
                                    response.getPercentage().toString(), response.getPath_to_cv()};

                            // insert TableRows in TableLayout
                            // get TableLayout container
                            TableLayout tableContainer = (TableLayout) getView().findViewById(R.id.profile_table);

                            for(int i = 0; i < STUDENT_TABLE_ROWS; i++){
                                // inflate TableRow
                                TableRow row = (TableRow) inflater.inflate(R.layout.profile_page_table, tableContainer, false);

                                // inflate element of col0 and set field, width
                                TextView col0Element = (TextView)inflater.inflate(R.layout.profile_table_row_element, row, false);
                                col0Element.setLayoutParams(new ViewGroup.LayoutParams(R.dimen.profile_col_0, ViewGroup.LayoutParams.WRAP_CONTENT));
                                col0Element.setText(field[i]);

                                // inflate element of col1 and set detail, width
                                TextView col1Element = (TextView)inflater.inflate(R.layout.profile_table_row_element, row, false);
                                col1Element.setLayoutParams(new ViewGroup.LayoutParams(R.dimen.profile_col_1, ViewGroup.LayoutParams.WRAP_CONTENT));
                                col1Element.setText(detail[i]);

                                // add col0 element to TableRow
                                row.addView(col0Element, 0);

                                // add col1 element to TableRow
                                row.addView(col1Element, 1);

                                // add TableRow to TableLayout
                                tableContainer.addView(row, i);
                            }
                        }

                        @Override
                        public void onFailure(HasuraException e) {
                            //Handle error
                            Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
        }

        else if(role[1].equals(getString(R.string.employer_role).toLowerCase())){
            EmployerProfileQuery query = new EmployerProfileQuery(user.getId());
            client.asRole(getString(R.string.employer_role).toLowerCase()).useDataService()
                    .setRequestBody(query)
                    .expectResponseType(ProfileResponse.class)
                    .enqueue(new Callback<ProfileResponse, HasuraException>() {
                        @Override
                        public void onSuccess(ProfileResponse response) {
                            // Handle response

                            // get headingContainer
                            LinearLayout headingContainer = (LinearLayout) getView().findViewById(R.id.name_and_institute);
                            // get inflater object
                            LayoutInflater inflater = LayoutInflater.from(getContext());

                            // place name
                            TextView headingName = (TextView) inflater.inflate(R.layout.profile_page_title, headingContainer, false);
                            headingName.setText(response.getName());
                            headingContainer.addView(headingName, 0);

                            // place company name and designation
                            TextView headingCompany = (TextView) inflater.inflate(R.layout.profile_page_title, headingContainer, false);
                            headingCompany.setText(response.getDesignation() + " at " + response.getCompany());
                            headingContainer.addView(headingCompany, 1);

                            String[] field = {"Name", "Email", "Date Of Birth", "Gender"};
                            String[] detail = {response.getName(), response.getEmail(), response.getDob(), response.getGender()};

                            // insert TableRows in TableLayout
                            // get TableLayout container
                            TableLayout tableContainer = (TableLayout) getView().findViewById(R.id.profile_table);

                            for(int i = 0; i < EMPLOYER_TABLE_ROWS; i++){
                                // inflate TableRow
                                TableRow row = (TableRow) inflater.inflate(R.layout.profile_page_table, tableContainer, false);

                                // inflate element of col0 and set field, width
                                TextView col0Element = (TextView)inflater.inflate(R.layout.profile_table_row_element, row, false);
                                col0Element.setLayoutParams(new ViewGroup.LayoutParams(R.dimen.profile_col_0, ViewGroup.LayoutParams.WRAP_CONTENT));
                                col0Element.setText(field[i]);

                                // inflate element of col1 and set detail, width
                                TextView col1Element = (TextView)inflater.inflate(R.layout.profile_table_row_element, row, false);
                                col1Element.setLayoutParams(new ViewGroup.LayoutParams(R.dimen.profile_col_1, ViewGroup.LayoutParams.WRAP_CONTENT));
                                col1Element.setText(detail[i]);

                                // add col0 element to TableRow
                                row.addView(col0Element, 0);

                                // add col1 element to TableRow
                                row.addView(col1Element, 1);

                                // add TableRow to TableLayout
                                tableContainer.addView(row, i);
                            }
                        }

                        @Override
                        public void onFailure(HasuraException e) {
                            //Handle error
                            Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

    }
}

这里是 FragmentPagerAdapter

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a UI (defined as a static inner class below).
        switch(position){
            case 0:
                return new ProfileFragment();

            case 1:
                return new MessagesFragment();

            case 2:
                return new HomeFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return getString(R.string.profile_tab);
            case 1:
                return getString(R.string.messages_tab);
            case 2:
                return getString(R.string.home_tab);
        }
        return null;
    }
}

除此之外还有另外 2 个 Fragment,我认为 post 不需要它们的代码。 提前致谢!

使用mViewPager.setOffscreenPageLimit(noOfPageYouWantToKeepInMemory);