Calling fragment method from activity Android

Jakub Riegel

I know this issue was discussed many times, but no solution works for me. I have an activity with fragment attached and using interface I'm calling a method from my activity that is replacing this fragment with another one. I want this method also to call a method from my new 'fragment', but however I implement that the application crashes when reaching this point. Does somebody know a solution for that? I'm new to Android development.

public void replacingMethod(String text){

    NewFragment newFragment = new NewFragment();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, newFragment).addToBackStack(null).commit();

    NewFragment f = (NewFragment) getSupportFragmentManager().findFragmentById(R.id.new_fragment);
    f.displayText(text);
}

I've tried to call displayText() from newFragment before and after replacement but it didn't work.

Anton Potapov

Your fragment should be attached to the activity to display text. The right way is to pass the text you want to show to the fragment using it's arguments with Fragment#setArguments(Bundle) and then get it inside fragment using Fragment#getArguments().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

NullPointerException when calling a Fragment method from Activity?

From

Calling a Fragment method from a parent Activity

From Dev

calling parent activity method from fragment

From Dev

NullPointer when calling Fragment method from activity

From Dev

Calling Activity from Fragment

From Dev

Calling fragment from activity

From Dev

Calling Fragment method from Activity,findFragmentById returns null

From Dev

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

From Dev

Calling Activity method inside Fragment that is not attached to Activity

From Dev

Calling a custom fragment from an activity

From Dev

android -does calling finish from activity destroy hosted fragment?

From Dev

calling an activity from fragment activity through Intent

From Dev

android from activity to fragment

From Dev

calling fragment method in Activity not working kotlin

From Dev

Call method of interface implements with child fragment From container activity Android

From Dev

Android; How to return a method for value from an activity inside a fragment?

From

Call an activity method from a fragment

From Dev

Fragment method not accessible from Activity

From Dev

Call Fragment method from Activity

From Dev

Calling a fragment method from an Adapter

From Dev

Calling method from Fragment in ViewPager

From Dev

Calling a Fragment method from a singleton

From Dev

Call method in fragment from another fragment/activity

From Dev

Calling Activity method from FirebaseMessagingService

From Dev

Calling a method from Activity in Adapter

From Dev

Calling a method from an exported activity

From Dev

Calling method of activity from asynctask

From Dev

App Crashing when calling a Fragment from an Activity

From Java

Calling a function when an activity is returned to from fragment

Related Related

  1. 1

    NullPointerException when calling a Fragment method from Activity?

  2. 2

    Calling a Fragment method from a parent Activity

  3. 3

    calling parent activity method from fragment

  4. 4

    NullPointer when calling Fragment method from activity

  5. 5

    Calling Activity from Fragment

  6. 6

    Calling fragment from activity

  7. 7

    Calling Fragment method from Activity,findFragmentById returns null

  8. 8

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

  9. 9

    Calling Activity method inside Fragment that is not attached to Activity

  10. 10

    Calling a custom fragment from an activity

  11. 11

    android -does calling finish from activity destroy hosted fragment?

  12. 12

    calling an activity from fragment activity through Intent

  13. 13

    android from activity to fragment

  14. 14

    calling fragment method in Activity not working kotlin

  15. 15

    Call method of interface implements with child fragment From container activity Android

  16. 16

    Android; How to return a method for value from an activity inside a fragment?

  17. 17

    Call an activity method from a fragment

  18. 18

    Fragment method not accessible from Activity

  19. 19

    Call Fragment method from Activity

  20. 20

    Calling a fragment method from an Adapter

  21. 21

    Calling method from Fragment in ViewPager

  22. 22

    Calling a Fragment method from a singleton

  23. 23

    Call method in fragment from another fragment/activity

  24. 24

    Calling Activity method from FirebaseMessagingService

  25. 25

    Calling a method from Activity in Adapter

  26. 26

    Calling a method from an exported activity

  27. 27

    Calling method of activity from asynctask

  28. 28

    App Crashing when calling a Fragment from an Activity

  29. 29

    Calling a function when an activity is returned to from fragment

HotTag

Archive