Value forwarding question to fragment.

Asked 2 years ago, Updated 2 years ago, 144 views

Currently, it consists of EditText and search buttons on one screen, three tabs, and a viewfager, each tab being a fragment. If you press search, you want to send the value that comes with json parsing to the fragment on the tab.

The code in the main.

                long id = parentJsonObject.getLong("id");
                STab1 stub1 = new STab1(); // STab1 is a fragment.
                Bundle bundle = new Bundle();
                bundle.putLong("id1", id);
                stab1.setArguments(bundle);

If you log here, the value will go over.

Below is the code in the fragment.

    if(getArguments() !=null) {
        Bundle bundle = getArguments();
        long id = bundle.getLong("id1");

        Log.e("aa", "Value : " + id");
    }

The value is not passed here. What should I do?

android-viewpager tablayout json android fragment

2022-09-22 20:44

3 Answers

I need to check a few things.


2022-09-22 20:44

I was writing an answer to the question you posted recently, but the question was deleted. If you haven't solved it, please refer to the information below.

When you click the color button in the first code block, onColorClick() seems to create a new fragment every time, is it the intended code? This will create a new fragment and will not be able to maintain its existing state. This is why it resets every time you click on a tab.

The UI you uploaded can be implemented easily using TabLayout. Please refer to the links below and modify the code.


2022-09-22 20:44

The main is currently using the View Pager tab layout toolbar. When you click Search here, you perform the SearchAsyncTask. Then, if you get a long value from SearchAsyncTask's onPostExecute, you want to send this long value to each tab ("ㄹ")") fragment to do another AsyncTask in the fragment. But I don't know how to send the long value to the fragment in the viewfager even if I look at the links <

This is the main code.

    searchBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = inputName.getText().toString();
            new SearchAsyncTask(name).execute ("URL");
        }
    });

    toolbar = (Toolbar) findViewById(R.id.searchToolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitle ("search");

    tabLayout = (TabLayout) findViewById(R.id.searchTabLayout);
    tabLayout.addTab(tabLayout.newTab().setText("m")"));
    tabLayout.addTab(tabLayout.newTab().setText("ㄴ")"));
    tabLayout.addTab(tabLayout.newTab().setText("ㄹ")"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    viewPager = (ViewPager) findViewById(R.id.searchViewPager);
    SearchViewPagerAdapter adapter = new SearchViewPagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    viewPager.setOffscreenPageLimit(3);

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

This is the viewfager adapter in the main function. How should I send the long value? <

private class SearchViewPagerAdapter extends FragmentStatePagerAdapter {

    int tabCount;

    public SearchViewPagerAdapter(FragmentManager fm, int tabCount) {
        super(fm);
        this.tabCount = tabCount;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return new STab1();
            case 1:
                return new STab2();
            case 2:
                return new STab3();
            default:
                return null;
        }
    }

    public Object instantiateItem(ViewGroup container, int position) {
        Fragment createdFragment = (Fragment) super.instantiateItem(container, position);

        switch (position) {
            case 0:
                sTab1 = (STab1) createdFragment;
                break;
            case 1:
                sTab2 = (STab2) createdFragment;
                break;
            case 2:
                sTab3 = (STab3) createdFragment;
                break;
        }
        return createdFragment;
    }

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


2022-09-22 20:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.