Calling a function when an activity is returned to from fragment

Lewis Smith :

I have a fragment that I call like so from my activities onCreate

android.app.FragmentManager fragmentManager = getFragmentManager();

DeviceControlFragment newFragment = new DeviceControlFragment();

newFragment.device = device;
fragmentManager.beginTransaction()
        .replace(R.id.content_frame, newFragment)
        .commit();

In the fragment I can either pop the back stack or hit back like so:

getFragmentManager().popBackStack();

OR

getActivity().onBackPressed();

But I need something in the activity so that I know we have returned there, is there something similar to onActivityResult that I can override in the activity to know when the fragment has been 'finished'

Zaid Mirza :

Declare a interface inside fragment and implement it in your activity and send response to activity inside onDetach(),onDestroy or onPaused method of fragment, any of them which suits you. I will prefer onDetach or onDestroy

Here is sample for that:

public class MyFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    private Test mListener;

    public interface Test{
        void imLeavingYou();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mListener = (Test) context;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        if(mListener != null){
            mListener.imLeavingYou();
        }
    }
}

And in your Activity do this:

public class MyActivity extends AppCompatActivity implements MyFragment.Test {

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

    @Override
    public void imLeavingYou() {
        // Okay thanks for informing
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

App Crashing when calling a Fragment from an Activity

From Dev

NullPointerException when calling a Fragment method from Activity?

From Dev

NullPointer when calling Fragment method from activity

From Dev

Calling Activity from Fragment

From Dev

Calling fragment from activity

From Dev

Calling a custom fragment from an activity

From Dev

calling an activity from fragment activity through Intent

From

Calling a Fragment method from a parent Activity

From Dev

Navigate back to calling Fragment from another activity

From Dev

calling parent activity method from fragment

From Dev

Calling Fragment from an activity and updating layout

From Dev

Calling fragment method from activity Android

From Dev

Error while calling Fragment class from activity

From Dev

Android - Calling a function from a fragment

From Dev

Should I pass Fragment instance or owner returned from getViewLifecycleOwner when calling setLifecycleOwner on data binding

From Dev

Android: How to get return result from activity when calling from Fragment?

From Dev

Calling fragment layout in an Activity

From Javascript

Calling a JavaScript function returned from an Ajax response

From Dev

Calling C function from returned pointer in NodeJS

From Dev

FragmentStateAdapter is not calling createfragment function when fragment changes

From Dev

How to call suspend function from Fragment or Activity?

From Dev

Lifecycle when calling the fragment replace or open new activity?

From Dev

Unhandled exception after reading from a binary file, when control is returned to calling function

From Dev

Crash when launching a new Activity from a Fragment

From Dev

android -does calling finish from activity destroy hosted fragment?

From Dev

Calling Fragment method from Activity,findFragmentById returns null

From Dev

Calling Fragment's method from Activity doesn't work?

From Dev

Calling JS function from Android Activity

From Dev

Send data from Activity to Fragment when fragment is onCreateView

Related Related

  1. 1

    App Crashing when calling a Fragment from an Activity

  2. 2

    NullPointerException when calling a Fragment method from Activity?

  3. 3

    NullPointer when calling Fragment method from activity

  4. 4

    Calling Activity from Fragment

  5. 5

    Calling fragment from activity

  6. 6

    Calling a custom fragment from an activity

  7. 7

    calling an activity from fragment activity through Intent

  8. 8

    Calling a Fragment method from a parent Activity

  9. 9

    Navigate back to calling Fragment from another activity

  10. 10

    calling parent activity method from fragment

  11. 11

    Calling Fragment from an activity and updating layout

  12. 12

    Calling fragment method from activity Android

  13. 13

    Error while calling Fragment class from activity

  14. 14

    Android - Calling a function from a fragment

  15. 15

    Should I pass Fragment instance or owner returned from getViewLifecycleOwner when calling setLifecycleOwner on data binding

  16. 16

    Android: How to get return result from activity when calling from Fragment?

  17. 17

    Calling fragment layout in an Activity

  18. 18

    Calling a JavaScript function returned from an Ajax response

  19. 19

    Calling C function from returned pointer in NodeJS

  20. 20

    FragmentStateAdapter is not calling createfragment function when fragment changes

  21. 21

    How to call suspend function from Fragment or Activity?

  22. 22

    Lifecycle when calling the fragment replace or open new activity?

  23. 23

    Unhandled exception after reading from a binary file, when control is returned to calling function

  24. 24

    Crash when launching a new Activity from a Fragment

  25. 25

    android -does calling finish from activity destroy hosted fragment?

  26. 26

    Calling Fragment method from Activity,findFragmentById returns null

  27. 27

    Calling Fragment's method from Activity doesn't work?

  28. 28

    Calling JS function from Android Activity

  29. 29

    Send data from Activity to Fragment when fragment is onCreateView

HotTag

Archive