Zeeshan Nazakat
Zeeshan Nazakat

Reputation: 31

Fragment not showing previous data when coming from another fragment

In my android activity I am using multiple fragments, I am successfully switching these fragments according to my requirement but problem is that when I am switching Fragment 1 to Fragment 2 and back from Fragment 2 to Fragment 1, Fragment 1 not showing previous data Fragment 1 start from stretch, but I want to show previous data same as I was selected.

Here is my code for go Fragment 1 (fragment_search_customerProfile) to Fragment 2 (fragment_CustomerInfo):

FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.filterFram, new fragment_Search());

FrameLayout layout = (FrameLayout) rootView.findViewById(R.id.cpFrame);
layout.removeAllViewsInLayout();

transaction.remove(new fragment_search_customerProfile()).commit();  

Here is my code for back Fragment 1 (fragment_search_customerProfile) fromFragment 2 (fragment_CustomerInfo):

FragmentManager manager = getActivity().getSupportFragmentManager();


FragmentTransaction transaction = manager.beginTransaction();

Log.d("fragment_Search", fromSearch + "");
transaction.replace(R.id.custIndoFram, new fragment_search_customerProfile());

FrameLayout layout = (FrameLayout) rootView.findViewById(R.id.custIndoFram);

layout.removeAllViewsInLayout();

transaction.remove(new fragment_CustomerInfo()).commit();

Can anyone explain me how can I stay save my fragment data?

Upvotes: 0

Views: 554

Answers (3)

scienticious
scienticious

Reputation: 438

You have to add fragment instead of remove or replace

Upvotes: 0

elmorabea
elmorabea

Reputation: 3263

You can simple keep the instances of the fragments, so in your Activity you would have fields like that.

private Fragment fragment1;
private Fragment fragment2;

And now you can use the sexy replace() option,

To add Fragment1

if (fragment1 == null) {
    fragment1 = new fragment_Search();
}
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.filterFram, fragment1);

and same for Fragment2

And you can hold the state of the fragment (data it retrieves) in the Fragment itself, and in your onViewCreated() you check, if the state is not null, you update the view immediately.

class Fragment1 extends Fragment {
    private State state;

    public void onViewCreated(View v) {
        if (state != null) {
            // Update UI here
        }
    }

}

UPDATE

Using your own code

private Fragment fragment1; // This is a field outside of below method


if (fragment1 == null) {
    fragment1 = new fragment_Search();
}
FragmentManager manager = getChildFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.filterFram, fragment1).commit();

FrameLayout layout = (FrameLayout) rootView.findViewById(R.id.cpFrame);
layout.removeAllViewsInLayout();

Going to fragment2 should be the same, and this code is used to go to Fragment1 and Fragment2, doesn't matter if you are going back or first time.

Upvotes: 0

Mark Samy
Mark Samy

Reputation: 148

Instead of transaction.replace(R.id.filterFram, new fragment_Search()); you can use transaction.add to add fragment while having the data of first one transaction.add(R.id.filterFram, new fragment_Search()); transaction.addToBackStack(null); From fragment two you can use .show instead of replace to show the first fragment and hide fragment two from the first one. transaction.show(getSupportFragmentManager().findFragmentByTag("firstFragmentTag")).commit();

Upvotes: 1

Related Questions