Sudarshan
Sudarshan

Reputation: 1036

Older Fragment is called when trying to access new Fragment data

I have four fragments FragmentZero, FragmentOne, FragmentTwo, FragmentThree. All the fragment have the same code except the data. When I trying to call the current fragment position it is calling the previous fragment as well as previous data. But the view is okay.

For example:

Until I reached the final fragment. But after reaching the last fragment everything is executing correctly.

Here is my Java code:

public class FragmentZero extends Fragment {

    private int position = 0;
    private ImageView art;

    private static final String TAG = FragmentZero.class.getSimpleName();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_drawing, container, false);

        art = root.findViewById(R.id.alphabet_image);
        art.setTag(position);
        art.setImageResource(DrawingActivity.imageList.get(position));

        final TextView textView = root.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_title, position + 1));

        ImageView draw_icon0 = root.findViewById(R.id.draw_icon);
        draw_icon0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), ActivityDraw.class).putExtra("image_res", DrawingActivity.imageList.get((Integer) art.getTag())));
                Log.d(TAG, (Integer) art.getTag()+" from TAG");
            }
        });
        return root;
    }

}

And the onCreate Code:

SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);

Adapter Code:

public class SectionsPagerAdapter extends FragmentPagerAdapter {
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

                switch (position) {
                    case 0:
                        return new FragmentZero();
                    case 1:
                        return new FragmentOne();
                    case 2:
                        return new FragmentTwo();
                    case 3:
                        return new FragmentThree();

                }


            return null;
        }

        @Override
        public int getCount() {
            return imageList.size();
        }
    }

I am trying to pass the current fragment value to the next activity.

Upvotes: 1

Views: 35

Answers (1)

CruxLab
CruxLab

Reputation: 26

As you are trying to pass current Fragment value to the next activity you can set the current position from your Activity.

Declare a variable e.g

public static int currentPos = 0;

Then set current value by

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                currentPos = position;
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

Now Call the current position from the Fragment & access the data from fragment using the currentPos.

Hopefully you will get the right value for

startActivity(new Intent(getContext(), ActivityDraw.class).putExtra("image_res", DrawingActivity.imageList.get(currentPos)));

Upvotes: 1

Related Questions