Problem sending data to viewpager using fragment

Asked 2 years ago, Updated 2 years ago, 152 views

When you run it, it is an app that displays a view page consisting of three fragments on top of the main activity. For the contents of each page, we received json parsing data and delivered it to each fragment In a single fragment other than a phaser, the normal expression was confirmed by delivering it to imageView and textView.

However, when you create and configure a viewpager with an adapter, the onCreateView of the fragment is loaded twice. The problem caused by this is that the compilation is successful based on the normally delivered data, but no content is displayed in textView and imageView.

If you do not use Fragment Manager, onCreateView is loaded only once, but there is a compilation error (NPE) when you setText to textView.

Eventually, Fragment Manager seems to be required to pass information from activities to fragments, but when configured as viewpager, onCreateView is called twice. If you take a picture with Log, there is no problem with the loading order, and I checked the normal operation when it is actually one fragment, so I think we only need to solve the problem when it is composed of viewpager, but I don't know how.

The viewpager is currently configured with the adapter in MainActivity.

[MainActivity]

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(R.string.select_name);
        setContentView(R.layout.activity_main);

        pager = (ViewPager) findViewById(R.id.pager);
        pager.setOffscreenPageLimit(3);

        PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());

        SelectFragment1 selectfragment1 = new SelectFragment1();
        adapter.addItem(selectfragment1);
        SelectFragment2 selectfragment2 = new SelectFragment2();
        adapter.addItem(selectfragment2);
        SelectFragment3 selectfragment3 = new SelectFragment3();
        adapter.addItem(selectfragment3);

        pager.setAdapter(adapter);

        selectFragment1 = new SelectFragment1();
        selectFragment2 = new SelectFragment2();
        selectFragment3 = new SelectFragment3();

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.pager, selectFragment1).commit();

        fragmentTransaction.replace(R.id.pager, selectFragment2);
        fragmentTransaction.replace(R.id.pager, selectFragment3);

        if (AppHelper.requestQueue == null) {
            AppHelper.requestQueue = Volley.newRequestQueue(getApplicationContext());
        }
    }

If you look at the logs in order, "Fragment 1", "OnCreatView" "Fragment 1", "OnCreatView" (occurring 2 times) "Fragment 2", "OnCreatView" "Fragment 3", "OnCreatView" "Fragment 1-response," called "List Area 1", "Called" "Fragment 2-response," called "List Area 2", "Called" "Fragment 3-response," called "List Area 3", "Called"

It's taken like this.

android fragment android-viewpager

2022-09-22 18:50

1 Answers

        SelectFragment1 selectfragment1 = new SelectFragment1();
        adapter.addItem(selectfragment1);
        SelectFragment2 selectfragment2 = new SelectFragment2();
        adapter.addItem(selectfragment2);
        SelectFragment3 selectfragment3 = new SelectFragment3();
        adapter.addItem(selectfragment3);

        pager.setAdapter(adapter);

        selectFragment1 = new SelectFragment1();
        selectFragment2 = new SelectFragment2();
        selectFragment3 = new SelectFragment3();

Create a fragment above and

Why are you creating another one below?


2022-09-22 18:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.