0

First let me try to reproduce my Problem/Question.

My Activity is a TabbedActivity with 3 Tabs, done with a SectionPageAdapter and a FragmentPageAdapter.

On these 3 Tabs the user is input Data. With just swiping the user can change the tab. After the User has filled in all data, he must click on a FloatingActionButton to save the data.

Now there is my Problem i don“t know if i can access these 3 Fragments with the filled in data ? Can i load all 3 Fragments with the filled in Data, read the data and save it or must i handle the PageChangeListenEvent and saved the Data before i change the Page?

Code:

 public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {

        return TravelInfoActivity.PlaceholderFragment.newInstance(position + 1);
    }

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

}


 public static class PlaceholderFragment extends Fragment {

    private static final String ARG_FRAGMENT_ID = "fragment_id";
    private static int REQUEST_CODE_PLACE = 1;
    private View currentRootView;
    private View currentClickedView;

    @SuppressLint("UseSparseArrays")
    private HashMap<Integer, View> hMap = new HashMap<>();


    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static TravelInfoActivity.PlaceholderFragment newInstance(int sectionNumber) {
        TravelInfoActivity.PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        int fragmentLayout;
        switch (sectionNumber) {
            case 1:
                fragmentLayout = R.layout.fragment_accommodation;
                break;
            case 2:
                fragmentLayout = R.layout.fragment_visited_places;
                break;
            case 3:
                fragmentLayout = R.layout.fragment_additional_info;
                break;
            default:
                fragmentLayout = R.layout.fragment_accommodation;
        }
        args.putInt(ARG_FRAGMENT_ID, fragmentLayout);
        fragment.setArguments(args);
        return fragment;
    }
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View rootView = inflater.inflate(getArguments().getInt(ARG_FRAGMENT_ID), container, false);
        currentRootView = rootView;
        switch (getArguments().getInt(ARG_FRAGMENT_ID)) {
            // switch handling here, not relevant
         }
 return rootView;
    }

1 Answers1

0

Guys i have done it refreshed my brain ;)

Just save the instance from the Fragment to an array and you can use it later. I made this in the SectionPageAdapter and ovverided the mehtod instantiateItem().

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

    private SparseArray<Fragment> registeredFragments = new SparseArray<>();

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


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        Integer fragmentId = fragment.getArguments().getInt("fragment_id");
        registeredFragments.put(fragmentToPageMap.get(fragmentId), fragment);

        return  fragment;
    }

    public Fragment getRegisteredFragment(int position) {
        return registeredFragments.get(position);
    }

    public boolean registeredFragmentExists(int position) {
        if(registeredFragments.size() >= position) {
            if(registeredFragments.get(position) != null) {
                return true;
            }
        }
        return false;
    }


    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
       return TravelInfoActivity.PlaceholderFragment.newInstance(position);
    }

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

}

Later you can access the fragments rootView with

mSectionsPagerAdapter.getRegisteredFragment(position);
fragment.getActivity().findViewById(id);