I would like to ask you about access to the function of Activity in Fragment.

Asked 2 years ago, Updated 2 years ago, 56 views

I understand that on Android, Fragment recommends that you implement it independently without any dependence on Activity so that it can be attached to any Activity and reused. As far as I know, the interface is implemented and approached in that way. So far, I understand.

// If you use it as below, the fragment is only available in OOActivity, so it should not be used!
((OOActivity) getActivity).OOActivityMethods();

By the way, the functions of the top-level activity class that can be obtained by getActivity(). For example, shouldn't I use functions such as .finish(), isFinishing(), etc.? Or what I understand is wrong.

I look forward to your reply!!

android fragment

2022-09-21 21:05

1 Answers

https://developer.android.com/reference/android/app/Activity.html

finish() Call this when your activity is done and should be closed.

isFinishing() Check to see whether this activity is in the process of finishing, either because you called finish() on it or someone else has requested that it finished.

Both functions are included in the Activity class, so there is no problem using them.

// If used as below, this fragment is only available in OOActivity, so it should not be used! ((OOActivity) getActivity).OOActivityMethods();

For strong coupling, the code is not reusable ((Activity) getActivity).OOActivityMethods();

Or

if(getActivity instanceof OOActivity){ ((OOActivity) getActivity).OOActivityMethods(); } You can use it after branching it out like this


2022-09-21 21:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.