0

I'm trying to switch a fragment when a button is pressed inside fragment itself and if an error is thrown by new fragment it will change back to the first fragment. I followed other answer, but none worked, what I get is always a blank fragment.

My main activity is a AppCompatActivity. This is a part of onCreate():

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    FragmentAdapter pageAdapter = new FragmentAdapter(getSupportFragmentManager(), MainActivity.this);
    viewPager.setAdapter(pageAdapter);
}

In main acivity I've this function to switch fragment

public void switchFragment(){
    Fragment fragment = null;
    try {
        fragment = SecondFragment.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }
    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.viewpager, fragment).commit();
}

And inside FirstFragment class:

((MainActivity) activityReference.get().getActivity()).switchFragment();

On startup the first fragmet is active and works well, when I press the button that call switchFragment() it become white. What did I miss?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
TheEnigmist
  • 906
  • 1
  • 10
  • 23
  • I found a solution. I created a root fragment with a FrameLayout and now I'm able to use correctly beginTransaction().replace() – TheEnigmist Jan 04 '18 at 19:58

2 Answers2

0

To change the fragment in your view pager, you can simply call currentItem method on viewpager. For e.g

viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
karandeep singh
  • 2,294
  • 1
  • 15
  • 22
  • Sorry, my fault, I wrote a "wrong" title. I've 4 fragment alredy setted in viewPager, what I need is to replace the fragment – TheEnigmist Jan 04 '18 at 18:20
  • just to be sure, you need to change it for a viewPager only. right? – karandeep singh Jan 04 '18 at 18:22
  • yeah. I've the same activity, with this viewPager. Inside this viewPager I've 4 fragment. 3 of them will be always the same and never changes. One of them need to be replaced when the button is pressed inside it. I get a white fragment. What can I see inside log console is that the fragmetn is correctly created but it is not attached to the viewPager for some reason! – TheEnigmist Jan 04 '18 at 18:26
-1

From your code your fragment = SecondFragment.newInstance(); line could be throwing an error in which case the fragment will be null when it reaches fragmentManager.beginTransaction().replace(R.id.viewpager, fragment).commit();, that could be causing you get a blank fragment.

Just making a null check before setting the fragment will ensure your first fragment remains visible in the case an error occurs:

if (fragment != null) fragmentManager.beginTransaction().replace(R.id.viewpager, fragment).commit();

Oscar Rene
  • 361
  • 2
  • 6