How can I pass data between a fragment and its container activity? Is there something similar to passing data between activities through intents?
I read this, but it didn't help much:
http://developer.android.com/guide/topics/fundamentals/fragments.html#CommunicatingWithActivity
Try using interfaces.
Any fragment that should pass data back to its containing activity should declare an interface to handle and pass the data. Then make sure your containing activity implements those interfaces. For example:
JAVA
In your fragment, declare the interface...
public interface OnDataPass {
public void onDataPass(String data);
}
Then, connect the containing class' implementation of the interface to the fragment in the onAttach method, like so:
OnDataPass dataPasser;
@Override
public void onAttach(Context context) {
super.onAttach(context);
dataPasser = (OnDataPass) context;
}
Within your fragment, when you need to handle the passing of data, just call it on the dataPasser object:
public void passData(String data) {
dataPasser.onDataPass(data);
}
Finally, in your containing activity which implements OnDataPass
...
@Override
public void onDataPass(String data) {
Log.d("LOG","hello " + data);
}
KOTLIN
Step 1. Create Interface
interface OnDataPass {
fun onDataPass(data: String)
}
Step 2. Then, connect the containing class' implementation of the interface to the fragment in the onAttach method (YourFragment), like so:
lateinit var dataPasser: OnDataPass
override fun onAttach(context: Context) {
super.onAttach(context)
dataPasser = context as OnDataPass
}
Step 3. Within your fragment, when you need to handle the passing of data, just call it on the dataPasser object:
fun passData(data: String){
dataPasser.onDataPass(data)
}
Step 4. Finally, in your activity implements OnDataPass
class MyActivity : AppCompatActivity(), OnDataPass {}
override fun onDataPass(data: String) {
Log.d("LOG","hello " + data)
}
In your fragment you can call getActivity()
.
This will give you access to the activity that created the fragment. From there you can obviously call any sort of accessor methods that are in the activity.
e.g. for a method called getResult()
on your Activity:
((MyActivity) getActivity()).getResult();
Easiest Approach but not Recommended
You can access activity data from fragment:
Activity:
public class MyActivity extends Activity {
private String myString = "hello";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
...
}
public String getMyData() {
return myString;
}
}
Fragment:
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MyActivity activity = (MyActivity) getActivity();
String myDataFromActivity = activity.getMyData();
return view;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle b = getActivity().getIntent().getExtras();
wid = b.getString("wid");
rid = b.getString("rid");
View view = inflater.inflate(R.layout.categoryfragment, container, false);
return view;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With