Viewpager Fragments turning blank
Asked Answered
A

5

6

Using a PagerSlidingTabStrip with a Viewpager inside of a fragment:

  • The main Activity contains a "main fragment" that changes depending on what item you click in navigation drawer.
  • When loading the initial fragment containing the Viewpager everything shows up fine (all pages are populated).
  • Replacing that main fragment with another one and then going back to the viewpager fragment turns every page in the viewpager blank, but the PagerSlidingTabStrip tabs are still there.

Any ideas?

Ability answered 26/3, 2015 at 11:0 Comment(0)
A
21

I had a problem like that

try this

mPager.setAdapter(new BasePagerAdapter(getChildFragmentManager(), getResources()));

you probably have this

mPager.setAdapter(new BasePagerAdapter(getFragmentManager(), getResources()));

EDIT: and in your BasePagerAdapter extend FragmentStatePagerAdapter

public class BasePagerAdapter extends FragmentStatePagerAdapter {
Amero answered 27/3, 2015 at 10:42 Comment(2)
I had the same problem and the same solution fixed itFervidor
i'm glad to be helpful :)Amero
V
1

Write ur code ie you are using for setting up your pager adapter inside

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    pager.setAdapter(adapter);
}
Verjuice answered 26/3, 2015 at 11:14 Comment(2)
I've tried forcing the adapter to redraw everything via notifydatasetchanged but it does not seem to work. I think it has to with the fragmentmanager. I've tried using getChildFragmentManager instead when initialising the pageradapter but it had no effect.Ability
You should use fragementsatepageadapter instead of fragmentpageadapterVerjuice
K
1

Replacing getFragmentManager() with getChildFragmentManager() helped me.

Kimmie answered 26/3, 2016 at 18:49 Comment(0)
S
0

Sorry to reply on an old post, but writing this because non of Stackoverflow solutions for my particular problem helped me.

If you are using new architecture components view model with a master detail shared view model and after returning from detail fragment get blank view pager, do the view model initialization in onViewCreated method of master fragment and not in onCreate (only needed in master fragment).

Also as other answers say remember to use childFragmentManager in view pager adapter.

like this:

class SharedViewModel : ViewModel() {
    val selected = MutableLiveData<Item>()

    fun select(item: Item) {
        selected.value = item
    }
}

class MasterFragment : Fragment() {

    private lateinit var itemSelector: Selector

    private lateinit var model: SharedViewModel

    // In the master fragment do the view model initialization in onViewCreated
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        model = activity?.run {
            ViewModelProviders.of(this).get(SharedViewModel::class.java)
        } ?: throw Exception("Invalid Activity")
        model.selected.observe(this, Observer<Item> { item ->
            // Update the UI
        })
    }
}

class DetailFragment : Fragment() {

    private lateinit var model: SharedViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        model = activity?.run {
            ViewModelProviders.of(this).get(SharedViewModel::class.java)
        } ?: throw Exception("Invalid Activity")
        model.selected.observe(this, Observer<Item> { item ->
            // Update the UI
        })
    }
}
Secular answered 16/3, 2019 at 0:16 Comment(0)
P
0

If you have multiple fragments under your main fragment A and move from Fragment B to Fragment A and this doesnt show the UI its only because you have been using getSupportFragmentManager() in your code, which was updating the parent fragment.

Instead of getSupportFragmentManager(), use: getChildFragmentManager() in your code.

Palembang answered 18/10, 2023 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.