问题,切换到另一个片段后清除了片段数据

时间:2019-01-09 02:11:49

标签: java android

我有一个 MainActivity ,其中有 x个片段,可以使用 viewpager 在它们之间进行切换。 他们每个人都使用 AsyncTaskLoader 发出网络请求,以检索一些数据。

对每个片段执行网络请求并检索数据,所有片段都显示正确的数据

问题是: 当我从Fragment_1切换到Fragment_2并尝试返回Fragment_1时,Fragment_1中的数据不再存在! 怎么来的???

以下是我的代码示例:

MainActivity

    ViewPager mviewpager = (ViewPager) findViewById(R.id.homeScreen_section_2_viewpager);

    SimpleFragmentPagerAdapter mFragmentAdapter = new SimpleFragmentPagerAdapter(this, getSupportFragmentManager());

    mviewpager.setAdapter(mFragmentAdapter);




    // find the tab layout that shows the tabs
    android.support.design.widget.TabLayout tabLayout = (android.support.design.widget.TabLayout) findViewById(R.id.homeScreen_section_2_sliding_tabs);
    tabLayout.setupWithViewPager(mviewpager);
    tabLayout.setTabMode(tabLayout.MODE_SCROLLABLE);

FragmentPagerAdapter

public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {


private Context mContext;

public SimpleFragmentPagerAdapter(Context context, FragmentManager fm) {
    super(fm);
    mContext = context;
}

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


@Override
public CharSequence getPageTitle(int position) {
    if (position == 0) {
        return mContext.getString(R.string.category_Upcoming);
    } else if (position == 1) {
        return mContext.getString(R.string.category_Latest);
    } else if (position == 2) {
        return mContext.getString(R.string.category_NowPlaying);
    } else if (position == 3) {
        return mContext.getString(R.string.category_TopRated);
    } else
        return mContext.getString(R.string.category_Popular);


}

@Override
public android.support.v4.app.Fragment getItem(int position) {
    if (position == 0) {
        return new UpComingFragment();
    } else if (position == 1) {
        return new LatestFragment();
    } else if (position == 2) {
        return new NowPlayingFragment();
    } else if (position == 3) {
        return new TopRatedFragment();
    } else
        return new PopularFragment();
}
}

Fragment_1

 // Get a reference to the LoaderManager, in order to interact with loaders.
        loaderManager = getActivity().getSupportLoaderManager();
        loaderManager.initLoader(List_item_LOADER_ID, null, List_Item_loaderCallbacks);
private android.support.v4.app.LoaderManager.LoaderCallbacks<List<Movie>> List_Item_loaderCallbacks
        = new android.support.v4.app.LoaderManager.LoaderCallbacks<List<Movie>>() {

@Override
    public android.support.v4.content.Loader<List<Movie>> onCreateLoader(int id, Bundle bundle) {

        if (id == List_item_LOADER_ID) {
            return new MovieLoader(getActivity(), TOP_RATED_REQUEST_URL);
        } else
            return null;
    }

    @Override
    public void 
onLoadFinished(android.support.v4.content.Loader<List<Movie>> loader, List<Movie> data) {
        loadingIndicator.setVisibility(View.GONE);
        emptyTextView.setText("Empty Movies List ");

        // Clear the adapter of previous Movies data
         mAdapter.clear();
         trailers_List.clear();
         movies_IDs_list.clear();

        // If there is a valid list of {@link Movie}s, then add them to the adapter's
        // data set. This will trigger the ListView to update.
        if (data != null && !data.isEmpty()) {

            int size = data.size();
            for (int i = 0; i < size; i++) {
                movies_IDs_list.add(data.get(i).getMovie_id());
            }
            mAdapter.addAll(data);

            // call the "TrailerLinkLoader" to get trailers Links for Retrieved movies
            loaderManager.initLoader(Trailer_Link_LOADER_ID, null, Trailer_loaderCallbacks);
        }
    }


    @Override
    public void onLoaderReset(android.support.v4.content.Loader<List<Movie>> loader) {
        mAdapter.clear();
        movies_IDs_list.clear();
        trailers_List.clear();
    }
};

Fragment_2 代码和其他Fragments 代码:与Fragment_1代码相似,因为它们都通过网络请求来检索给定的不同 Url 的数据

1 个答案:

答案 0 :(得分:3)

使用mViewPager.setOffscreenPageLimit(NUMBER_OF_TAB -1);保持片段状态。无论如何,这将需要更多的内存