Pass data between fragments

2

Well I need the following: I have an application that will Acticity this is the main and controls the entire application, I also have some fragments 2 of them are interface other 2 run in the background.

The question is to pass data from FragmentA to FragmentB , but the moment I try to do this, I tried to object tried getter and setter tried Bundle e intent but error persists .

The idea is then to make the fragment1 pass data pro fragment2 the main activity contains running in the background.

Does anyone help me?

    
asked by anonymous 08.08.2016 / 04:43

3 answers

2

Speak Roger,

To pass data between the fragments, do the following, at the moment you are going to use FragmentMeneger you have to create a Bundle and pass along, for example:

Fragment myFrag = new MyFragment();

Bundle bundle = new Bundle();
bundle.putString("username", user_ame);
bundle.putString("senha", user_pass);
myFrag.setArguments(bundle);

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, myFrag).commit();

Hugs.

    
08.08.2016 / 14:32
2

The Bundle only passes data to another Fragment if it does not pass through the attach. Fragment does not communicate directly with another Fragment, Fragment has to communicate with Activity and Activity communicates with Fragment B (this if the 2 Fragments are Up and running), so you have to use an interface.

example:

1) Interface

public interface OnCommunicateInterface
{
    void onSetText(String str);
}

2) MainActivity

public class MainActivity extends FragmentActivity implements OnCommunicateInterface
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onSetText(String str)
    {
        FragmentReceiveData f2 = (FragmentReceiveData) getSupportFragmentManager().findFragmentById(R.id.frag_rec_data);
        f2.updateText(str);
    }

}

3) Fragment that will send Data

public class FragmentSendData extends Fragment
{
    EditText editText;
    Button button;
    OnCommunicateInterface onCommunicate;

    @Override
    /****************** onCreateView *****************/
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b)
    {
        View v = inflater.inflate(R.layout.fragment_send_data,container,false);

        editText = (EditText)v.findViewById(R.id.editText);
        button = (Button)v.findViewById(R.id.button);

       View.OnClickListener listener = new View.OnClickListener()
       {
           @Override
           public void onClick(View v)
           {
               String str = editText.getText().toString();
               onCommunicate.onSetText(str);
           }
       };

       button.setOnClickListener(listener);

       // Inflate the layout for this fragment
       return (v);
    }

    @Override
    /**************** onAttach **************/
    public void onAttach(Context context)
    {
        super.onAttach(context);

        try
        {
            onCommunicate = (OnCommunicateInterface) context;
        }
        catch (Exception e)
        {
            Log.e("onAttach",e.toString());
        }
    }
}

4) Fragment that will receive Date

public class FragmentReceiveData extends Fragment
{
    TextView textView;

    @Override
    /************** onCreateView() ******************/
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.fragment_receive_data,container,false);

        textView = (TextView)v.findViewById(R.id.text_view);

        // Inflate the layout for this fragment
        return (v);
    }
    /*************** updateText() ********************/
    public void updateText(String string)
    {
        textView.setText(string);
    }
}

The cidogo is a bit ugly, but to be easy to understand and a simple example.

I hope it helps.

    
10.08.2016 / 06:05
0

There are several ways to do this.

Shared Preferences Stores proven primitive data in key-value pairs.

Internal Storage Stores private data in device memory.

SQLite Databases Stores structured data in a private database.

And also as Leonardo Dias posted using the Bundle. There it goes of its utility and know which to use.

    
08.08.2016 / 14:38