2

I am using Fragment for my current application. Here I have one Fragment FA and I am navigating to the other Fragment FB and I have added FA to the backstack as I want to come to FA fragment on some event to be done in FB fragment. I have a done button in the fragment FB. On Click of that button I need to do two things in the fragment FB which are as follows :-

(a). I need to finish the current fragment FB. (b). Secondly, I need to pass some data from FB to already existing Fragment FA as I have added it to backstack.

I have just started using Fragments and don't know much about them and I want to sort out this problem.Can anyone suggest something for this, any Help would be appreciable.

Thanks in Advance.

Salman Khan
  • 2,822
  • 5
  • 32
  • 53
  • 1
    here [Communicating Fragments](http://developer.android.com/training/basics/fragments/communicating.html) – Spurdow Aug 11 '14 at 05:46

1 Answers1

0

You can pass data from one fragment to another something like below.

  Fragment fragment;
    fragment = new SingleImage();
    Bundle bundle = new Bundle();
    bundle.putString("img",actorsList.get(position).getImage());
    bundle.putString("desc",actorsList.get(position).getDesc());
    fragment.setArguments(bundle);
    getActivity().getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).addToBackStack( "tag" ).commit();

means you can add arguments with fragment to pass to another.

and you can get that data in another fragment using below code

    Bundle bundle = this.getArguments();

    if(bundle != null)
    {
        tvdesc.setText(bundle.getString("desc"));
        img_url= bundle.getString("img");
     }

Since you want only one back stack entry per Fragment, make the back state name the Fragment's class name (via getClass().getName()). Then when replacing a Fragment, use the popBackStackImmediate() method. If it returns true, it means there is an instance of the Fragment in the back stack. If not, actually execute the Fragment replacement logic.

 private void replaceFragment (Fragment fragment){
 String backStateName = fragment.getClass().getName();

 FragmentManager manager = getSupportFragmentManager();
 boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);

 if (!fragmentPopped){ //fragment not in back stack, create it.
    FragmentTransaction ft = manager.beginTransaction();
    ft.replace(R.id.content_frame, fragment);
    ft.addToBackStack(backStateName);
    ft.commit();
   }
  }
Paras Valera
  • 602
  • 4
  • 13
  • 1
    But my Fragment which is getting the data is already in backstack and I have to refresh the list when user press the Done button. – Salman Khan Aug 11 '14 at 05:49
  • How can I refresh my list if the fragment containing list is already in the backstack? – Salman Khan Aug 11 '14 at 05:50
  • i did not get what you want exactly but i have edited my answer see if it help you. – Paras Valera Aug 11 '14 at 05:57
  • Can you go through my question once again, I didn't get you edited answer. – Salman Khan Aug 11 '14 at 06:26
  • A) finish the current fragment FB you can use this link https://gist.github.com/jankovd/9303738 and B)to pass some data from FB to already existing Fragment FA you can write above code in to onclick of done button i have given where you can pass and get data through bundle. – Paras Valera Aug 11 '14 at 14:09