Bruno Pardini
Bruno Pardini

Reputation: 55

Fragment disappears using SlidingTabLayout

I am using a SlidingTabLayout to implement the sliding tabs. The problem is, when backing from a fragment to the tab fragment, it disappears.

I am going to show the application flow to make things more clear.

First, I call an Activity whithin a Fragment:

public class ScreenSlidePageFragment extends Fragment{
    ...
    public View onCreateView(args...){
        ...
        gridView.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                switch(position){
                    case 0:
                        Intent intent = new Intent(getActivity(), FrequencyActivity.class);
                        startActivity(intent);
                        break;
                }
            }
        }
        ...
    }
}

The code of the FrequencyActivity is below:

public class FrequencyActivity extends AppCompatActivity{
    ...
    protected void onCreate(Bundle savedInstance){
        ...
        toolbar.setNavigationOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
                onBackPressed();
            }
        });    
    }

    final FragmentManager fm = getSupportFragmentManager();

    Fragment fragment = Fragment.instantiate(getBaseContext(), "com.example.Fragment.FragmentFrequency");

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.home, fragment, "FragFrequency");
    fragmentTransaction.addToBackStack("frequency");
    fragmentTransaction.commit();

    fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
                finish(); //When there is no back stack, finish the activity
            } else {
                //Testing purpose
                int top = fm.getBackStackEntryCount();
                Log.d("BACKSTACK", "Backstack count: " + String.valueOf(top));
                Log.d("BACKSTACK", "Backstack name: " + fm.getBackStackEntryAt(top - 1).getName());
            }
        }
    });
}

The FragmentFrequency is the one which contains the SlidingTabLayout, the code can be seen below:

public class FragmentFrequency extends Fragment{
    ...
    //Creates ViewPager adapter
    adapter = new ViewPagerAdapter(activity.getSupportFragmentManager(), titles, numOfTabs);

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

   //SlidingTabLayout code
   ...
}

And finally, the ViewPagerAdapter which loads the Fragments of the tabs

public class ViewPagerAdapter extends FragmentStatePagerAdapter{
    ...
    @Override
    public Fragment getItem(int position){
        if(position == 0)
            return new FragmentTab1();
        else
            return new FragmentTab2();
    }
    ...
}

For example when the first tab is selected, the FragmentTab1 is loaded, which contains:

Fragment f = Fragment.instantiate(getActivity(), "com.example.Fragment.FragmentLaunchingFrequency");
FragmentTransaction tx = getActivity().getSupportFragmentManager().beginTransaction();
tx.replace(R.id.home, f, "FragLaunchingFrequency");
tx.addToBackStack("launchingfrequency");
tx.commit();

The problem is, when the back action is done, the FrequencyActivity loses the reference of the Fragment and it shows a blank. Also, the sliding tabs stop working properly.

Does anyone know how to fix this? I am really out of alternatives.

Thanks

Upvotes: 0

Views: 360

Answers (1)

The Original Android
The Original Android

Reputation: 6215

I think you have 2 major questions in your post. Perhaps make another post for the other question. For now, I can address your question "The problem is, when the back action is done, the FrequencyActivity loses the reference of the Fragment and it shows a blank". Your code:

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.home, fragment, "FragFrequency");
fragmentTransaction.addToBackStack("frequency");
fragmentTransaction.commit();

fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
    @Override
    public void onBackStackChanged() {
    ...

Notes:

  • You cannot call addToBackStack() and use OnBackStackChangedListener() in the same FragmentManager. This seems complicated.

Code suggestions:

  • Remove the use of addOnBackStackChangedListener() and see what happens.

Specific code suggestion:

fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
                finish(); //When there is no back stack, finish the activity
            } else {
// Call FragmentManager.findFragmentByTag(String tag)
// Call FragmentTransaction.show() after getting the Fragment.

            }
        }
    });

Note: Notice the else block manages the Fragment instead of depending on the BackStack (addToBackStack method).

Upvotes: 1

Related Questions