简体   繁体   中英

Recreate fragments in FragmentPagerAdapter

I'm creating application which has FragmentPagerAdapter with two pages.

The class for FragmentPagerAdapter looks like this

public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int i) {
        Fragment fragment;
        switch (i) {
            case 0: fragment = new CurrentRateFragment(); break;
            case 1: fragment = new HistoryFragment(); break;
            default: fragment = new CurrentRateFragment(); break;
        }

        return fragment;
    }

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

I want that some changes on first page (for example change Spinner selected item) caused changes on second page.

As I've read about Fragment communication ( https://developer.android.com/training/basics/fragments/communicating.html ) and understand that fragments can communicate only through Activity.

For this case I've created public interface in my first page class fragment

public interface CurrencyListener {
    public void onCurrencyChanged();
}

And implement it in my Activity.

Now I can call void onCurrencyChanged in my Activity from my first page fragment.

But the problem is:

How to recreate second page fragment in my FragmentPagerAdapter?

Fragment creation operation is heavy.

1. If you recreate fragment the all the views will be created and previous Fragment needs to be garbage collected. Therefore there will extra memory.

Therefore rather than recreating the FirstPageFragment just refresh the data based on the callback received on your currencyChanged() method. In this case, fragment will be created once and data will be updated every time the currencyChanged() method is called.

HistoryFragment Code(Fragment to be refreshed)

public class HistoryFragment extends Fragment implements MyActivity.IUpdateData{
    /**
     * Other method goes here
     */

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //
        if(getActivity() instanceof /**Your Activity**/){
            ((/**Your Activity**/)getActivity()).setOnUpdateListener(this);
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void updateData(Object object) {
        //Refresh Data
    }
}

Activity (Which will refresh data)

public class MyActivity extends Activity {
    /**
     * Other method goes here
     */
    private IUpdateData dataUpdateListener;

    public void setOnUpdateListener(IUpdateData listener){
        dataUpdateListener = listener;
    }


    public void onCurrencyChanged(){
        if(dataUpdateListener!=null){
            dataUpdateListener.updateData(/**Update Data**/);
        }
    }

        public interface IUpdateData{
            void updateData(Object o);
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM