11

I am making an app for car finding.I am showing result in gridview and also have a sliding menu on right hand side(for search filter).I have made this whole app with the help of fragments.In the gridview I have a load more footer at bottom, when I click on 12th position gridview , I am starting another fragments like this:

SearchDetailActivity.goToFragment(ProductDetailFragment.newInstance(map,position));

and in ProductDetailFragment I have a back button,to go back on previous fragment like this:

SearchDetailActivity.goToFragment(SearchDetailFragment.newInstance(str_url));

Now when I came back again data shows from 0-10th item.

In goToFragment() I have write this code:

public static void goToFragment(Fragment fragment) {
    Log.d("GoToFrag","sjdk>>"+fragment);
    Fragment tmp = fm.findFragmentByTag(fragment.getClass().getName());
    if (tmp != null && tmp.isVisible())
        return;

    ft = fm.beginTransaction();
    ft.replace(R.id.main_fragment, fragment, fragment.getClass().getName());
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
    currentTag = fragment.getClass().getName();
}

I want to avoid data reload when come back from ProductDetailFragment.If I used Activity then I can use onBackPressed() to back and avoid reloading but in Fragment when I pressed app's back button ,It reload the SearchDetatilFragment,which is very annoying..Please help me..Thanks in advance.

1

2 Answers 2

40

I have stumbled on this issue for about 3 months. Finally my efforts were paid off and found a solution.

The problem is, when you user "replace", this is equivalent to "remove and add". so the fragment is completely removed and recreated when back button is pressed. instead we have to hide the parent fragment and show the child.

Where serach fragment is called

 ft = fm.beginTransaction();
 ft.replace(R.id.main_fragment, yourSearchFragment, "searchFragment");
 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
 ft.commit();

When calling productDetails fragment

 ft = fm.beginTransaction();
 ft.hide(getFragmentManager().findFragmentByTag("searchFragment"));
 ft.add(R.id.main_fragment, yourDetailfragment);
 ft.addToBackStack(null);
 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
 ft.commit();

Good luck

4
  • Thanks a lot. Good Answer. Worked Like a charm Aug 23, 2015 at 19:28
  • I have a function static can't use beginTransaction
    – user3402040
    Mar 15, 2016 at 11:43
  • what if you want to change title on back presss ?
    – Rajesh N
    Jun 24, 2017 at 3:42
  • I am calling like this EventCardFragment artistFragment = new EventCardFragment(); FragmentManager manager = getSupportFragmentManager(); manager.beginTransaction().setCustomAnimations(R.anim.slide_in_right,R.anim.slide_out_left,R.anim.slide_in_left, R.anim.slide_out_right).replace(R.id.frame_container, artistFragment) .addToBackStack(ArtistDetailFragment.class.getName()).commit(); but on back press or call to getFragmentManager().popBackStack(); it always reload the default fragment so what should I do? even I have tried .add instead to replace then Animation won't work. Jan 4, 2021 at 10:51
-2

Use getFragmentManager().popBackStack() on back pressed of fragment, this will load previous fragment and not create new one.

For your apps back button set up a listener like this:

View.OnClickListener mBackListener = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();

    }
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.