Calling a custom fragment from an activity

Tal Angel

I creating my own Fragment that I wanna activate from my activity. My mainActivity file is a pure Activity (not a FragmantActivity or anything else). Why I can't use my custom Fragment as a parameter?

if (savedInstanceState == null) {
        // Add the fragment on initial activity setup
        myFragment = new CustomFragment();
        getFragmentManager()
        .beginTransaction()
                .add(android.R.id.content, myFragment )
                .commit();

I am getting a compile error telling me that the 2nd parameter on .add() method must be a Fragment, but 'myFragment' is a CustomFragment that derives Fragment class. So how to activate this fragment from a normal Activity?

Ponsuyambu Velladurai

Make sure that you're consistent with your Fragment imports. Use either import android.app.Fragment in all classes or import android.support.v4.app.Fragment; in all classes. Don't mix-and-match

FragmentActivity or any child class of FragmentActivity only can hold the fragment from the support library. Hence change your activity's parent class to FragmentActivity, it will work. As AppCompatActivity inherits from FragmentActivity it also will work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling a function when an activity is returned to from fragment

From

Calling a Fragment method from a parent Activity

From Dev

android -does calling finish from activity destroy hosted fragment?

From Dev

Android: Send ArrayList of custom objects from fragment to activity using Parcerable

From Dev

Calling fragment method from activity Android

From Dev

Error while calling Fragment class from activity

From Dev

Calling Activity method inside Fragment that is not attached to Activity

From Dev

Starting a fragment from an Activity

From Dev

Passing a Custom Type Object From Fragment to Activity with Navigation Component

From Dev

Calling Fragment from an activity and updating layout

From Dev

NullPointer when calling Fragment method from activity

From Dev

calling an activity method from custom onClickListener class

From Dev

Calling fragment from activity

From Dev

android from activity to fragment

From Dev

Android: Calling a function inside a fragment from a custom action bar

From Dev

calling an activity from fragment activity through Intent

From Dev

calling parent activity method from fragment

From Dev

Fragment menu calling activity menu

From Dev

Calling Fragment method from Activity,findFragmentById returns null

From Dev

NullPointerException when calling a Fragment method from Activity?

From Dev

Calling a Class Fragment which extends Fragment from a MainActivity Class which extends Activity

From Dev

Navigate back to calling Fragment from another activity

From Dev

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

From Dev

App Crashing when calling a Fragment from an Activity

From Dev

How to pass custom object arraylist from activity to fragment with instance

From Dev

Calling Activity from Fragment

From Dev

Calling fragment layout in an Activity

From Dev

How to Pass Custom arraylist from fragment to activity

From Dev

How to pass custom objects from an activity to a fragment?

Related Related

  1. 1

    Calling a function when an activity is returned to from fragment

  2. 2

    Calling a Fragment method from a parent Activity

  3. 3

    android -does calling finish from activity destroy hosted fragment?

  4. 4

    Android: Send ArrayList of custom objects from fragment to activity using Parcerable

  5. 5

    Calling fragment method from activity Android

  6. 6

    Error while calling Fragment class from activity

  7. 7

    Calling Activity method inside Fragment that is not attached to Activity

  8. 8

    Starting a fragment from an Activity

  9. 9

    Passing a Custom Type Object From Fragment to Activity with Navigation Component

  10. 10

    Calling Fragment from an activity and updating layout

  11. 11

    NullPointer when calling Fragment method from activity

  12. 12

    calling an activity method from custom onClickListener class

  13. 13

    Calling fragment from activity

  14. 14

    android from activity to fragment

  15. 15

    Android: Calling a function inside a fragment from a custom action bar

  16. 16

    calling an activity from fragment activity through Intent

  17. 17

    calling parent activity method from fragment

  18. 18

    Fragment menu calling activity menu

  19. 19

    Calling Fragment method from Activity,findFragmentById returns null

  20. 20

    NullPointerException when calling a Fragment method from Activity?

  21. 21

    Calling a Class Fragment which extends Fragment from a MainActivity Class which extends Activity

  22. 22

    Navigate back to calling Fragment from another activity

  23. 23

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

  24. 24

    App Crashing when calling a Fragment from an Activity

  25. 25

    How to pass custom object arraylist from activity to fragment with instance

  26. 26

    Calling Activity from Fragment

  27. 27

    Calling fragment layout in an Activity

  28. 28

    How to Pass Custom arraylist from fragment to activity

  29. 29

    How to pass custom objects from an activity to a fragment?

HotTag

Archive