Marcus Gruneau
Marcus Gruneau

Reputation: 827

Viewpager Fragments turning blank

Using a PagerSlidingTabStrip with a Viewpager inside of a fragment:

Any ideas?

Upvotes: 6

Views: 4258

Answers (5)

Prajwal Waingankar
Prajwal Waingankar

Reputation: 2710

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.

Upvotes: 0

Pooya
Pooya

Reputation: 89

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
        })
    }
}

Upvotes: 0

ViliusK
ViliusK

Reputation: 11555

Replacing getFragmentManager() with getChildFragmentManager() helped me.

Upvotes: 1

xanexpt
xanexpt

Reputation: 753

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 {

Upvotes: 21

Akash
Akash

Reputation: 711

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);
}

Upvotes: 1

Related Questions