Transferring data from a fragment to an activity from another activity

I investigated the transfer of data from Activity to Activity. But I could not use the same method to go to my fragment. How to transfer data to my fragment?

The code I use

Intent i = new Intent(getApplicationContext(), NewActivity.class); i.putExtra("new_variable_name","value"); startActivity(i); 

and in other activities

  Bundle extras = getIntent().getExtras(); if (extras != null) { String value = extras.getString("new_variable_name"); } 
+4
source share
2 answers

Its pretty easy to transfer data between two fragments using an interface.

I think this one is what you are looking for.

Hope this helps :)

To pass a date from an action to a fragment, this is well explained here .

+3
source
  • Use setArguments() to pass the Bundle to the fragment. The snippet will retain this package even when the configuration is changed. It will be delivered to onCreate() and other fragment methods.

  • Find the existing fragment by its identifier or tag: getFragmentManager.findFragmentById() and call the fragment class method.

  • Save the link to the parent activity in a local variable in the onAttach() fragment and call the methods of the parent activity class from the fragment.

0
source

Source: https://habr.com/ru/post/1486545/


All Articles