Pass data between Fragments

Asked

Viewed 4,673 times

3

Here’s what I need: I have an applicationActicitythis is the main and controls the entire application, also I have some fragments2 of them are interface other 2 run in the background.

The question is the following need to pass data fromFragmentAto the FragmentB,only that the moment I try to make this mistake, I tried to guide the objecttried getter and settertried Bundle e intentbut the error persists.

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

someone help me?

  • What do you mean, "I tried to steer the Object"? What error persists?

  • What kind of data do you want to pass on? At what point do you want to pass it on?

  • I try to access a method that is in another Ragment(these 2 Ragments are visible on the screen) and it returns me an error saying that I can’t access, I tried with thread and even then it didn’t work

  • See if this help, otherwise you have to give more details about what you want to do.

3 answers

3

Bundle only dates to another Fragment if the same has not passed through the attach. Fragment does not communicate directly with another Fragment , Fragment has to communicate with Activity and Activity communicates with Fragment B (if the 2 Fragments are Up and running), for that 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 Date

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 to 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 rather ugly , but it is easy to understand and it is a simple example.

hope it helps .

2

Fala Roger,

To pass data between Fragments, do the following, the moment you use Fragmentmeneger you have to create a Bundle and pass together, 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.

  • Hi Leonardo, is the following, the Bundle method works if I change the Fragment, but these my Fragments they are already created and at runtime or they are already being shown on the screen. also need to draw one or more methods within a Fragment at a certain time, paid attention

1

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 how Leonardo Dias posted using the Bundle. There goes your usefulness and know which to use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.