avoid reloading on back pressed in fragments
Asked Answered
H

2

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.

Henze answered 31/3, 2014 at 9:42 Comment(1)
You would be interested in developer.android.com/reference/android/app/…Dictator
D
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

Dysfunction answered 9/2, 2015 at 15:45 Comment(3)
I have a function static can't use beginTransactionKonstanze
what if you want to change title on back presss ?Insular
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.Seger
K
-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();

    }
};
Kedgeree answered 31/3, 2014 at 10:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.