Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh a Fragment of a FragmentPagerAdapter?

I have got 2 tabs called tab1 (home) and tab2 (profile) using TabPageIndicator and FragmentPagerAdapter libraries.

In tab2, I have a button named "Log in" (tab2 inflates abc.xml).

When users click "log in" button, the app will show a login form in a new activity. After logging in, the activity finishes and the content view of tab2 will change ( it shows user's profile because it will inflate xyz.xml). How can I do that?

public class MainActivity extends SherlockFragmentActivity {
    private static String[] TAB_NAMES = null;
    private Context context = this;
    private static FragmentPagerAdapter adapter ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        if (TAB_NAMES == null) {
            TAB_NAMES = new String[] {
                    getResources().getString(R.string.tab_home),
                    getResources().getString(R.string.tab_profile) };
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adapter = new TabsAdapter(
                getSupportFragmentManager());

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

        pager.setAdapter(adapter);


        TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
        indicator.setViewPager(pager);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater infalter = getSupportMenuInflater();
        infalter.inflate(R.menu.main_menu, menu);
        return true;
    }

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

        @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0:
                return HomeFragment.getInstance();
            case 1:
                return new ProfileFragment();

            default:
                return HomeFragment.getInstance();
            }
        }
        }
like image 203
EastBK Avatar asked Sep 10 '13 02:09

EastBK


People also ask

How do I refresh FragmentPagerAdapter?

You can use startActivityForResult(or broadcast/receiver) to get the result from the activity. Then use adapter. notifyDataSetChanged to refresh the fragment.

How do I refresh a ViewPager fragment?

OnPageChangeListener is the correct way to go, but you will need to refactor your adapter a bit in order to keep a reference to each Fragment contained in the FragmentPagerAdapter. Then, instead of creating a new Fragment, use the one contained in the adapter: mViewPager. addOnPageChangeListener(new ViewPager.

How do I refresh a fragment from another fragment?

You can access another Fragment by its tag: // find your fragment YourFragment f = (YourFragment) getSupportFragmentManager(). findFragmentByTag("yourFragTag"); // update the list view f. updateListView();

How do you reattach a fragment?

when your activity is recreated, the fragment gets destroyed. So you have to create new instance of fragment and add it again. Ok, but you cant retain the fragment view. If you want to retain the data which is in fragment, then use onSaveInstanceState() and onRestoreInstanceState() .


2 Answers

I was also facing the same issue, So i resolved it on my way:

Just refresh your HomeFragment or ProfileFragment by overriding onResume method.

In onResume Method you can refresh your ListFragment or Fragment easily.

like image 192
Min2 Avatar answered Sep 21 '22 16:09

Min2


You can use FragmentStatePagerAdapter instead of FragmentPagerAdapter

You can try like this:

private static FragmentStatePagerAdapter adapter ;
private static String[] TAB_NAMES = null;

@Override
protected void onCreate(Bundle savedInstanceState) {

    if (TAB_NAMES == null) {
        TAB_NAMES = new String[] {
                getResources().getString(R.string.tab_home),
                getResources().getString(R.string.tab_profile) };
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    adapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return HomeFragment.getInstance();
                case 1:
                    return new ProfileFragment();

                default:
                    return HomeFragment.getInstance();
            }
        }

        @Override
        public int getCount() {
            return TAB_NAMES.length;
        }

        @Override
        public int getItemPosition(Object fragItem) {
            int position = 0;
            if (fragItem instanceof HomeFragment) {
                position = 0;
            } else if (fragItem instanceof  ProfileFragment) {
                position = 1;
            }
            return (position >= 0) ? position : POSITION_NONE;
        }
    }

    ViewPager pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);


    TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
    indicator.setViewPager(pager);

}

Reference

You can check this solution.

like image 29
missionMan Avatar answered Sep 25 '22 16:09

missionMan