0

I had asked one question before some day i.e. : Using fragment more than one time with new values with backstack maintain

My problem has been sought out ..but the old fragment is refreshing(loading) ..which is not required(as it contains paging) .I know the reason of refreshing the old fragment that is I have used FragmentTransaction.replace in goToFragmentWithBackStack method.If I will used FragmentTransaction.add in goToFragmentWithBackStack method, it will not work any more and the new data will be updated in the old fragment.

Something like this : A(old data)->B->A(new data) [AS I WANT ..EXPECTED RESULT] but shows if I used FragmentTransaction.replace in goToFragmentWithBackStack method: A(old data with loading/refreshing)->B->A(new data with loading/refreshing)

If I used FragmentTransaction.add in goToFragmentWithBackStack method: A(old data)->B->nothing...

If I check the backstack on back button pressed it shows something like :

If I used FragmentTransaction.replace in goToFragmentWithBackStack method: A(old data with loading/refreshing)<-B<-A(new data with loading/refreshing)(I WANT TO STOP REFRESHING THE OLD FRAGMENT)

If I used FragmentTransaction.add in goToFragmentWithBackStack method: A(with new data and old fragment)<-B<-A(blank nothing) (As old fragment A already is in container hence new data is setting in old fragment)

CONCLUTION: My main problem is that I want to stop refreshing data in old Fragment A on Back Pressed. With FragmentTransaction.add in goToFragmentWithBackStack method ,it is not working...

Here is my code :

public static void goToFragmentWithBackStack(Fragment fragment) {
    Fragment tmp = fm.findFragmentByTag(fragment.getClass().getName());
    if (tmp != null && tmp.isVisible())
        return;

    ft = fm.beginTransaction();

    ft.replace(R.id.content_frame, fragment, fragment.getClass().getName());
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(null);
    ft.commit();
    currentTag = fragment.getClass().getName();
}

calling fragment:

  LeftAndRightActivity.goToFragmentWithBackStack(new ExploreFragment(theWord,"#"));

The ExploreFragment:

public ExploreFragment(String data,String pattern) {
this.str_data = data;   
this.str_pattern = pattern;
}

Now the flow of app is something like that I am going from ExploreFragment(1) to PostDetailFragment and then again ExploreFragment(2) with new data..(works fine)

Now when I returned back on pressing back button of device the old ExploreFragment(1) is loading again...I don't want to refresh the data of ExploreFragment(1) it should be only just resumed not refreshed...I hope you understand my problem..

Please help me..Thanks in advance..

Community
  • 1
  • 1
userAndroid
  • 586
  • 2
  • 9
  • 26
  • add some code of the FragmentTranscation.replace function.... Ideally I will pass data to the new fragment as argument at the time of creation and u can retrieve that in the fragment using getArgument() method – droid kid Dec 11 '14 at 08:19
  • hello I have add same code which is in the link.. – userAndroid Dec 11 '14 at 10:12
  • I think you are not getting my point..I have add all these functionalties successfully with "replace" method..I want to just "add" same fragment more than one time instead of "replace" – userAndroid Dec 11 '14 at 10:14
  • yes.. you can add the same fragment n number of times with different instance using the "replace" method – droid kid Dec 11 '14 at 10:53
  • paste the code which you are using – droid kid Dec 11 '14 at 10:55
  • I don't want to use fragmenttransaction.replace method...I want to use fragmenttransaction.add method...As using "replace" method is refreshing the fragment everytime ..I don't want to refresh fragment ...which can be done be using "add" method ..but when I use "add" method ..I am facing some problems which I have mentioned in my question... – userAndroid Dec 11 '14 at 11:29
  • friend the whole thing you written is confusing and I dont want to waste my time reading this. thas y I asked for the code u used for navigating to fragment. Also the link you have given doesnt have the working code. Better you rephrase the question – droid kid Dec 11 '14 at 12:27

1 Answers1

0

In your goToFragmentWithBackStack() method, you are using the class name as tag. So FragmentA should always have the same tag even if you create a new instance. So give a unique tag to Fragment and check whether the values are retaining.

or

you remove the if() condition and always create a new fragment instance

public static void goToFragmentWithBackStack(Fragment fragment) {            
    ft = fm.beginTransaction();

    ft.add(R.id.content_frame, fragment, fragment.getClass().getName());
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(null);
    ft.commit();
    currentTag = fragment.getClass().getName();
}

I didnt try out this code so check this.

droid kid
  • 7,569
  • 2
  • 32
  • 37