The problem is I want to go to another fragment by clicking item on list view, but when I replace fragment it overlaps with the previous fragment! I cannot hide the previous fragment! My code the following:

package com.harar.khalil.quraan.fragments;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;

import com.harar.khalil.quraan.R;
import com.harar.khalil.quraan.databases.DatabaseHelp;
import com.harar.khalil.quraan.interfaces.FragmentListener;

public class Pager extends FragmentStatePagerAdapter {

    //integer to count number of tabs
    private int tabCount;
    private DatabaseHelp databaseHelp;
    private FragmentManager fragmentManager;
    private FrameLayout frameLayout1,frameLayout;
    //Constructor to the class 
    public Pager(FragmentManager fm, int tabCount, DatabaseHelp databaseHelp, FrameLayout frameLayout) {
        super(fm);
        //Initializing tab count
        this.fragmentManager=fm;
        this.tabCount= tabCount;
        this.databaseHelp=databaseHelp;
        this.frameLayout=frameLayout;
    }

    //Overriding method getItem
    @Override
    public Fragment getItem(int position) {
        //Returning the current tabs 
        switch (position) {
            case 0:
                 final SuraFragment suraFragment=SuraFragment.newInstance(databaseHelp);


                 suraFragment.setOnFragmentListener(new FragmentListener() {
                    @Override
                    public void OnclickItem(DatabaseHelp databaseHelp, int id, String oromo) {
                      QuranFragment quranFragment=QuranFragment.newInstance(databaseHelp,id,oromo);
                      String tag = suraFragment.getClass().getName();
                      FragmentTransaction transaction=fragmentManager.beginTransaction();
                      fragmentManager.popBackStack();
                      transaction.replace(R.id.fragment_container,quranFragment,tag);
                      transaction.addToBackStack(tag);
                      transaction.commit();
                    }
                });
                return suraFragment;
            case 1:
                return MuzhafFragment.newInstance();

            case 2:
                return new BookMarkFragment();
            default:
                return null;
        }
    }

    //Overriden method getCount to get the number of tabs 
    @Override
    public int getCount() {
        return tabCount;
    }
}

the output is the image below

enter image description here

0 Answers