Communicate from Activity to Fragment using Interface












2















I have searched SO for this problem but was not able to find anything which would solve my problem. My problem is, I have a activity which contains FrameLayout which is constantly updated with different fragments. The top view and bottom view are going to remain same hence they are in the layout of the
activity.



As you can see bottom view has a button on click of that i want to make changes in the fragments which will be present in the FrameLayout.



enter image description here



I have created a interface



public interface ShowFormula {

void showFormula(boolean show);

}


which i will use to implement in the fragment.
Now the main problem in my MainActivity class i am trying to initialize the interface but not able to as i am getting class cast exception



showFormula = (ShowFormula) this;//yes i know this is wrong


How should i initialize this in order to communicate with the fragment.
Main goal is to toggle the view in fragments on click of the button in activity.



Thanks in advance.










share|improve this question


















  • 1





    Implement your interface on the fragment and assign it to your interface variable on fragment creation. You are doing it in reverse order it doesnt make sense to store a reference to this and it crash because you have implemented the interface in the fragment (Thats OK).

    – Nanoc
    Nov 11 '15 at 16:34













  • but how will i get button click listener event in fragment as the button is present in the layout of the activity. Thats y i have tried to implement in reverse order, so when user clicks on the button present in the activity i could toggle visibility of the view present in the fragment.

    – Swapnil
    Nov 11 '15 at 16:40











  • Use the interface to notify your fragment of the button click

    – Nanoc
    Nov 11 '15 at 16:41






  • 2





    You don't need to use an interface to make calls from an activity to a fragment. Just keep a reference to the current fragment, and call into a public method in the fragment.

    – Daniel Nugent
    Nov 11 '15 at 16:44











  • yes that's what i was trying to do but i am getting class cast exception in Activity as i am not able to initialize the interface. Please refer to the last code snippet in my question. :)

    – Swapnil
    Nov 11 '15 at 16:45
















2















I have searched SO for this problem but was not able to find anything which would solve my problem. My problem is, I have a activity which contains FrameLayout which is constantly updated with different fragments. The top view and bottom view are going to remain same hence they are in the layout of the
activity.



As you can see bottom view has a button on click of that i want to make changes in the fragments which will be present in the FrameLayout.



enter image description here



I have created a interface



public interface ShowFormula {

void showFormula(boolean show);

}


which i will use to implement in the fragment.
Now the main problem in my MainActivity class i am trying to initialize the interface but not able to as i am getting class cast exception



showFormula = (ShowFormula) this;//yes i know this is wrong


How should i initialize this in order to communicate with the fragment.
Main goal is to toggle the view in fragments on click of the button in activity.



Thanks in advance.










share|improve this question


















  • 1





    Implement your interface on the fragment and assign it to your interface variable on fragment creation. You are doing it in reverse order it doesnt make sense to store a reference to this and it crash because you have implemented the interface in the fragment (Thats OK).

    – Nanoc
    Nov 11 '15 at 16:34













  • but how will i get button click listener event in fragment as the button is present in the layout of the activity. Thats y i have tried to implement in reverse order, so when user clicks on the button present in the activity i could toggle visibility of the view present in the fragment.

    – Swapnil
    Nov 11 '15 at 16:40











  • Use the interface to notify your fragment of the button click

    – Nanoc
    Nov 11 '15 at 16:41






  • 2





    You don't need to use an interface to make calls from an activity to a fragment. Just keep a reference to the current fragment, and call into a public method in the fragment.

    – Daniel Nugent
    Nov 11 '15 at 16:44











  • yes that's what i was trying to do but i am getting class cast exception in Activity as i am not able to initialize the interface. Please refer to the last code snippet in my question. :)

    – Swapnil
    Nov 11 '15 at 16:45














2












2








2


2






I have searched SO for this problem but was not able to find anything which would solve my problem. My problem is, I have a activity which contains FrameLayout which is constantly updated with different fragments. The top view and bottom view are going to remain same hence they are in the layout of the
activity.



As you can see bottom view has a button on click of that i want to make changes in the fragments which will be present in the FrameLayout.



enter image description here



I have created a interface



public interface ShowFormula {

void showFormula(boolean show);

}


which i will use to implement in the fragment.
Now the main problem in my MainActivity class i am trying to initialize the interface but not able to as i am getting class cast exception



showFormula = (ShowFormula) this;//yes i know this is wrong


How should i initialize this in order to communicate with the fragment.
Main goal is to toggle the view in fragments on click of the button in activity.



Thanks in advance.










share|improve this question














I have searched SO for this problem but was not able to find anything which would solve my problem. My problem is, I have a activity which contains FrameLayout which is constantly updated with different fragments. The top view and bottom view are going to remain same hence they are in the layout of the
activity.



As you can see bottom view has a button on click of that i want to make changes in the fragments which will be present in the FrameLayout.



enter image description here



I have created a interface



public interface ShowFormula {

void showFormula(boolean show);

}


which i will use to implement in the fragment.
Now the main problem in my MainActivity class i am trying to initialize the interface but not able to as i am getting class cast exception



showFormula = (ShowFormula) this;//yes i know this is wrong


How should i initialize this in order to communicate with the fragment.
Main goal is to toggle the view in fragments on click of the button in activity.



Thanks in advance.







java android android-fragments android-activity






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 '15 at 16:33









SwapnilSwapnil

1,94311931




1,94311931








  • 1





    Implement your interface on the fragment and assign it to your interface variable on fragment creation. You are doing it in reverse order it doesnt make sense to store a reference to this and it crash because you have implemented the interface in the fragment (Thats OK).

    – Nanoc
    Nov 11 '15 at 16:34













  • but how will i get button click listener event in fragment as the button is present in the layout of the activity. Thats y i have tried to implement in reverse order, so when user clicks on the button present in the activity i could toggle visibility of the view present in the fragment.

    – Swapnil
    Nov 11 '15 at 16:40











  • Use the interface to notify your fragment of the button click

    – Nanoc
    Nov 11 '15 at 16:41






  • 2





    You don't need to use an interface to make calls from an activity to a fragment. Just keep a reference to the current fragment, and call into a public method in the fragment.

    – Daniel Nugent
    Nov 11 '15 at 16:44











  • yes that's what i was trying to do but i am getting class cast exception in Activity as i am not able to initialize the interface. Please refer to the last code snippet in my question. :)

    – Swapnil
    Nov 11 '15 at 16:45














  • 1





    Implement your interface on the fragment and assign it to your interface variable on fragment creation. You are doing it in reverse order it doesnt make sense to store a reference to this and it crash because you have implemented the interface in the fragment (Thats OK).

    – Nanoc
    Nov 11 '15 at 16:34













  • but how will i get button click listener event in fragment as the button is present in the layout of the activity. Thats y i have tried to implement in reverse order, so when user clicks on the button present in the activity i could toggle visibility of the view present in the fragment.

    – Swapnil
    Nov 11 '15 at 16:40











  • Use the interface to notify your fragment of the button click

    – Nanoc
    Nov 11 '15 at 16:41






  • 2





    You don't need to use an interface to make calls from an activity to a fragment. Just keep a reference to the current fragment, and call into a public method in the fragment.

    – Daniel Nugent
    Nov 11 '15 at 16:44











  • yes that's what i was trying to do but i am getting class cast exception in Activity as i am not able to initialize the interface. Please refer to the last code snippet in my question. :)

    – Swapnil
    Nov 11 '15 at 16:45








1




1





Implement your interface on the fragment and assign it to your interface variable on fragment creation. You are doing it in reverse order it doesnt make sense to store a reference to this and it crash because you have implemented the interface in the fragment (Thats OK).

– Nanoc
Nov 11 '15 at 16:34







Implement your interface on the fragment and assign it to your interface variable on fragment creation. You are doing it in reverse order it doesnt make sense to store a reference to this and it crash because you have implemented the interface in the fragment (Thats OK).

– Nanoc
Nov 11 '15 at 16:34















but how will i get button click listener event in fragment as the button is present in the layout of the activity. Thats y i have tried to implement in reverse order, so when user clicks on the button present in the activity i could toggle visibility of the view present in the fragment.

– Swapnil
Nov 11 '15 at 16:40





but how will i get button click listener event in fragment as the button is present in the layout of the activity. Thats y i have tried to implement in reverse order, so when user clicks on the button present in the activity i could toggle visibility of the view present in the fragment.

– Swapnil
Nov 11 '15 at 16:40













Use the interface to notify your fragment of the button click

– Nanoc
Nov 11 '15 at 16:41





Use the interface to notify your fragment of the button click

– Nanoc
Nov 11 '15 at 16:41




2




2





You don't need to use an interface to make calls from an activity to a fragment. Just keep a reference to the current fragment, and call into a public method in the fragment.

– Daniel Nugent
Nov 11 '15 at 16:44





You don't need to use an interface to make calls from an activity to a fragment. Just keep a reference to the current fragment, and call into a public method in the fragment.

– Daniel Nugent
Nov 11 '15 at 16:44













yes that's what i was trying to do but i am getting class cast exception in Activity as i am not able to initialize the interface. Please refer to the last code snippet in my question. :)

– Swapnil
Nov 11 '15 at 16:45





yes that's what i was trying to do but i am getting class cast exception in Activity as i am not able to initialize the interface. Please refer to the last code snippet in my question. :)

– Swapnil
Nov 11 '15 at 16:45












4 Answers
4






active

oldest

votes


















6














You don't need to use an interface to make calls from an Activity to a Fragment. Just keep a reference to the current Fragment, and call into a public method in the Fragment from the Activity.



If you have multiple Fragments and you don't want to keep a reference for each one, you can create a Fragment base class, declare the common method in the base class, and then implement that method override in all of your Fragments that inherit from the base Fragment. Then, keep one reference of the base Fragment type, and always have it set to the Fragment that is shown currently.






share|improve this answer



















  • 1





    Yes...did exactly that way. Thanks @Daniel Nugent

    – Swapnil
    Nov 12 '15 at 8:11



















2














a clean solution:



`public interface ShowFormula {
public void showFormula(boolean show);
}`

`public class MyActivity implements ShowFormula {
...
@Override
public void showFormula(boolean show) {
/** Your Code **/
}
...
}`

`public class MyFragment {
private ShowFormula listener;
...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
listener = (ShowFormula) activity;
// listener.showFormula(show?);
} catch (ClassCastException castException) {
/** The activity does not implement the listener. **/
}
}
...
}`





share|improve this answer



















  • 1





    This is Fragment -> Activity, not Activity -> Fragment

    – Linxy
    Jan 15 '17 at 20:21



















0














Activity to Fragment Communication via Interface:



public class MyActivity {

private ShowFormula showFormulaListener;

public interface ShowFormula {
public void showFormula(boolean show);
}

public void setListener(MyFragment myFragment) {
try {
showFormulaListener = myFragment;
} catch(ClassCastException e) {
}
}
}

public class MyFragment implements ShowFormula{

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
((MyActivity) activity).setDebugListener(this);
} catch (ClassCastException e) {
Log.e(TAG, e.toString());
}
}
@Override
public void showFormula(boolean show) {
/** Your Code **/
}
}


Once you are done setting this, you can call 'showFormulaListener.showFormula(boolVal)'






share|improve this answer































    0














    Activity ---> Fragment




    Communication from Activity to Fragment is pretty straightforward. You
    really don't need a listener.




    Let's say you have a method inside Fragment share()



    public class MyFragment extends Fragment{

    public static MyFragment getInstance()
    {
    return new MyFragment();
    }

    ........

    public void share()
    {
    // do something
    }

    }


    How to call share() method from an Activity?




    Get the reference of the Fragment and call the method. Simple!




    MyFragment myFragment = MyFragment.getInstance();
    myFragment.share();


    You can see the full working code for Fragment to Fragment Communication






    share|improve this answer


























    • Cannot resolve getInstance()

      – user1804084
      Jan 23 at 14:14











    • I was trying to show a minimal code. getInstance() is how you instantiate a Fragment. But for you I have updated the code. @user1804084 . Go through the git repository for better understanding.

      – Rohit Singh
      Jan 23 at 15:01













    • If you are wondering why I create instance this way. You can read this post stackoverflow.com/questions/9245408/…

      – Rohit Singh
      Jan 23 at 15:03













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33655239%2fcommunicate-from-activity-to-fragment-using-interface%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    You don't need to use an interface to make calls from an Activity to a Fragment. Just keep a reference to the current Fragment, and call into a public method in the Fragment from the Activity.



    If you have multiple Fragments and you don't want to keep a reference for each one, you can create a Fragment base class, declare the common method in the base class, and then implement that method override in all of your Fragments that inherit from the base Fragment. Then, keep one reference of the base Fragment type, and always have it set to the Fragment that is shown currently.






    share|improve this answer



















    • 1





      Yes...did exactly that way. Thanks @Daniel Nugent

      – Swapnil
      Nov 12 '15 at 8:11
















    6














    You don't need to use an interface to make calls from an Activity to a Fragment. Just keep a reference to the current Fragment, and call into a public method in the Fragment from the Activity.



    If you have multiple Fragments and you don't want to keep a reference for each one, you can create a Fragment base class, declare the common method in the base class, and then implement that method override in all of your Fragments that inherit from the base Fragment. Then, keep one reference of the base Fragment type, and always have it set to the Fragment that is shown currently.






    share|improve this answer



















    • 1





      Yes...did exactly that way. Thanks @Daniel Nugent

      – Swapnil
      Nov 12 '15 at 8:11














    6












    6








    6







    You don't need to use an interface to make calls from an Activity to a Fragment. Just keep a reference to the current Fragment, and call into a public method in the Fragment from the Activity.



    If you have multiple Fragments and you don't want to keep a reference for each one, you can create a Fragment base class, declare the common method in the base class, and then implement that method override in all of your Fragments that inherit from the base Fragment. Then, keep one reference of the base Fragment type, and always have it set to the Fragment that is shown currently.






    share|improve this answer













    You don't need to use an interface to make calls from an Activity to a Fragment. Just keep a reference to the current Fragment, and call into a public method in the Fragment from the Activity.



    If you have multiple Fragments and you don't want to keep a reference for each one, you can create a Fragment base class, declare the common method in the base class, and then implement that method override in all of your Fragments that inherit from the base Fragment. Then, keep one reference of the base Fragment type, and always have it set to the Fragment that is shown currently.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 11 '15 at 18:34









    Daniel NugentDaniel Nugent

    34.3k1082113




    34.3k1082113








    • 1





      Yes...did exactly that way. Thanks @Daniel Nugent

      – Swapnil
      Nov 12 '15 at 8:11














    • 1





      Yes...did exactly that way. Thanks @Daniel Nugent

      – Swapnil
      Nov 12 '15 at 8:11








    1




    1





    Yes...did exactly that way. Thanks @Daniel Nugent

    – Swapnil
    Nov 12 '15 at 8:11





    Yes...did exactly that way. Thanks @Daniel Nugent

    – Swapnil
    Nov 12 '15 at 8:11













    2














    a clean solution:



    `public interface ShowFormula {
    public void showFormula(boolean show);
    }`

    `public class MyActivity implements ShowFormula {
    ...
    @Override
    public void showFormula(boolean show) {
    /** Your Code **/
    }
    ...
    }`

    `public class MyFragment {
    private ShowFormula listener;
    ...
    @Override
    public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
    listener = (ShowFormula) activity;
    // listener.showFormula(show?);
    } catch (ClassCastException castException) {
    /** The activity does not implement the listener. **/
    }
    }
    ...
    }`





    share|improve this answer



















    • 1





      This is Fragment -> Activity, not Activity -> Fragment

      – Linxy
      Jan 15 '17 at 20:21
















    2














    a clean solution:



    `public interface ShowFormula {
    public void showFormula(boolean show);
    }`

    `public class MyActivity implements ShowFormula {
    ...
    @Override
    public void showFormula(boolean show) {
    /** Your Code **/
    }
    ...
    }`

    `public class MyFragment {
    private ShowFormula listener;
    ...
    @Override
    public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
    listener = (ShowFormula) activity;
    // listener.showFormula(show?);
    } catch (ClassCastException castException) {
    /** The activity does not implement the listener. **/
    }
    }
    ...
    }`





    share|improve this answer



















    • 1





      This is Fragment -> Activity, not Activity -> Fragment

      – Linxy
      Jan 15 '17 at 20:21














    2












    2








    2







    a clean solution:



    `public interface ShowFormula {
    public void showFormula(boolean show);
    }`

    `public class MyActivity implements ShowFormula {
    ...
    @Override
    public void showFormula(boolean show) {
    /** Your Code **/
    }
    ...
    }`

    `public class MyFragment {
    private ShowFormula listener;
    ...
    @Override
    public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
    listener = (ShowFormula) activity;
    // listener.showFormula(show?);
    } catch (ClassCastException castException) {
    /** The activity does not implement the listener. **/
    }
    }
    ...
    }`





    share|improve this answer













    a clean solution:



    `public interface ShowFormula {
    public void showFormula(boolean show);
    }`

    `public class MyActivity implements ShowFormula {
    ...
    @Override
    public void showFormula(boolean show) {
    /** Your Code **/
    }
    ...
    }`

    `public class MyFragment {
    private ShowFormula listener;
    ...
    @Override
    public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
    listener = (ShowFormula) activity;
    // listener.showFormula(show?);
    } catch (ClassCastException castException) {
    /** The activity does not implement the listener. **/
    }
    }
    ...
    }`






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 11 '15 at 18:31









    jMikejMike

    12439




    12439








    • 1





      This is Fragment -> Activity, not Activity -> Fragment

      – Linxy
      Jan 15 '17 at 20:21














    • 1





      This is Fragment -> Activity, not Activity -> Fragment

      – Linxy
      Jan 15 '17 at 20:21








    1




    1





    This is Fragment -> Activity, not Activity -> Fragment

    – Linxy
    Jan 15 '17 at 20:21





    This is Fragment -> Activity, not Activity -> Fragment

    – Linxy
    Jan 15 '17 at 20:21











    0














    Activity to Fragment Communication via Interface:



    public class MyActivity {

    private ShowFormula showFormulaListener;

    public interface ShowFormula {
    public void showFormula(boolean show);
    }

    public void setListener(MyFragment myFragment) {
    try {
    showFormulaListener = myFragment;
    } catch(ClassCastException e) {
    }
    }
    }

    public class MyFragment implements ShowFormula{

    @Override
    public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
    ((MyActivity) activity).setDebugListener(this);
    } catch (ClassCastException e) {
    Log.e(TAG, e.toString());
    }
    }
    @Override
    public void showFormula(boolean show) {
    /** Your Code **/
    }
    }


    Once you are done setting this, you can call 'showFormulaListener.showFormula(boolVal)'






    share|improve this answer




























      0














      Activity to Fragment Communication via Interface:



      public class MyActivity {

      private ShowFormula showFormulaListener;

      public interface ShowFormula {
      public void showFormula(boolean show);
      }

      public void setListener(MyFragment myFragment) {
      try {
      showFormulaListener = myFragment;
      } catch(ClassCastException e) {
      }
      }
      }

      public class MyFragment implements ShowFormula{

      @Override
      public void onAttach(Activity activity) {
      super.onAttach(activity);
      try {
      ((MyActivity) activity).setDebugListener(this);
      } catch (ClassCastException e) {
      Log.e(TAG, e.toString());
      }
      }
      @Override
      public void showFormula(boolean show) {
      /** Your Code **/
      }
      }


      Once you are done setting this, you can call 'showFormulaListener.showFormula(boolVal)'






      share|improve this answer


























        0












        0








        0







        Activity to Fragment Communication via Interface:



        public class MyActivity {

        private ShowFormula showFormulaListener;

        public interface ShowFormula {
        public void showFormula(boolean show);
        }

        public void setListener(MyFragment myFragment) {
        try {
        showFormulaListener = myFragment;
        } catch(ClassCastException e) {
        }
        }
        }

        public class MyFragment implements ShowFormula{

        @Override
        public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
        ((MyActivity) activity).setDebugListener(this);
        } catch (ClassCastException e) {
        Log.e(TAG, e.toString());
        }
        }
        @Override
        public void showFormula(boolean show) {
        /** Your Code **/
        }
        }


        Once you are done setting this, you can call 'showFormulaListener.showFormula(boolVal)'






        share|improve this answer













        Activity to Fragment Communication via Interface:



        public class MyActivity {

        private ShowFormula showFormulaListener;

        public interface ShowFormula {
        public void showFormula(boolean show);
        }

        public void setListener(MyFragment myFragment) {
        try {
        showFormulaListener = myFragment;
        } catch(ClassCastException e) {
        }
        }
        }

        public class MyFragment implements ShowFormula{

        @Override
        public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
        ((MyActivity) activity).setDebugListener(this);
        } catch (ClassCastException e) {
        Log.e(TAG, e.toString());
        }
        }
        @Override
        public void showFormula(boolean show) {
        /** Your Code **/
        }
        }


        Once you are done setting this, you can call 'showFormulaListener.showFormula(boolVal)'







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 13:54









        AnubhavAnubhav

        46968




        46968























            0














            Activity ---> Fragment




            Communication from Activity to Fragment is pretty straightforward. You
            really don't need a listener.




            Let's say you have a method inside Fragment share()



            public class MyFragment extends Fragment{

            public static MyFragment getInstance()
            {
            return new MyFragment();
            }

            ........

            public void share()
            {
            // do something
            }

            }


            How to call share() method from an Activity?




            Get the reference of the Fragment and call the method. Simple!




            MyFragment myFragment = MyFragment.getInstance();
            myFragment.share();


            You can see the full working code for Fragment to Fragment Communication






            share|improve this answer


























            • Cannot resolve getInstance()

              – user1804084
              Jan 23 at 14:14











            • I was trying to show a minimal code. getInstance() is how you instantiate a Fragment. But for you I have updated the code. @user1804084 . Go through the git repository for better understanding.

              – Rohit Singh
              Jan 23 at 15:01













            • If you are wondering why I create instance this way. You can read this post stackoverflow.com/questions/9245408/…

              – Rohit Singh
              Jan 23 at 15:03


















            0














            Activity ---> Fragment




            Communication from Activity to Fragment is pretty straightforward. You
            really don't need a listener.




            Let's say you have a method inside Fragment share()



            public class MyFragment extends Fragment{

            public static MyFragment getInstance()
            {
            return new MyFragment();
            }

            ........

            public void share()
            {
            // do something
            }

            }


            How to call share() method from an Activity?




            Get the reference of the Fragment and call the method. Simple!




            MyFragment myFragment = MyFragment.getInstance();
            myFragment.share();


            You can see the full working code for Fragment to Fragment Communication






            share|improve this answer


























            • Cannot resolve getInstance()

              – user1804084
              Jan 23 at 14:14











            • I was trying to show a minimal code. getInstance() is how you instantiate a Fragment. But for you I have updated the code. @user1804084 . Go through the git repository for better understanding.

              – Rohit Singh
              Jan 23 at 15:01













            • If you are wondering why I create instance this way. You can read this post stackoverflow.com/questions/9245408/…

              – Rohit Singh
              Jan 23 at 15:03
















            0












            0








            0







            Activity ---> Fragment




            Communication from Activity to Fragment is pretty straightforward. You
            really don't need a listener.




            Let's say you have a method inside Fragment share()



            public class MyFragment extends Fragment{

            public static MyFragment getInstance()
            {
            return new MyFragment();
            }

            ........

            public void share()
            {
            // do something
            }

            }


            How to call share() method from an Activity?




            Get the reference of the Fragment and call the method. Simple!




            MyFragment myFragment = MyFragment.getInstance();
            myFragment.share();


            You can see the full working code for Fragment to Fragment Communication






            share|improve this answer















            Activity ---> Fragment




            Communication from Activity to Fragment is pretty straightforward. You
            really don't need a listener.




            Let's say you have a method inside Fragment share()



            public class MyFragment extends Fragment{

            public static MyFragment getInstance()
            {
            return new MyFragment();
            }

            ........

            public void share()
            {
            // do something
            }

            }


            How to call share() method from an Activity?




            Get the reference of the Fragment and call the method. Simple!




            MyFragment myFragment = MyFragment.getInstance();
            myFragment.share();


            You can see the full working code for Fragment to Fragment Communication







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 23 at 15:00

























            answered Jan 1 at 12:04









            Rohit SinghRohit Singh

            3,19722934




            3,19722934













            • Cannot resolve getInstance()

              – user1804084
              Jan 23 at 14:14











            • I was trying to show a minimal code. getInstance() is how you instantiate a Fragment. But for you I have updated the code. @user1804084 . Go through the git repository for better understanding.

              – Rohit Singh
              Jan 23 at 15:01













            • If you are wondering why I create instance this way. You can read this post stackoverflow.com/questions/9245408/…

              – Rohit Singh
              Jan 23 at 15:03





















            • Cannot resolve getInstance()

              – user1804084
              Jan 23 at 14:14











            • I was trying to show a minimal code. getInstance() is how you instantiate a Fragment. But for you I have updated the code. @user1804084 . Go through the git repository for better understanding.

              – Rohit Singh
              Jan 23 at 15:01













            • If you are wondering why I create instance this way. You can read this post stackoverflow.com/questions/9245408/…

              – Rohit Singh
              Jan 23 at 15:03



















            Cannot resolve getInstance()

            – user1804084
            Jan 23 at 14:14





            Cannot resolve getInstance()

            – user1804084
            Jan 23 at 14:14













            I was trying to show a minimal code. getInstance() is how you instantiate a Fragment. But for you I have updated the code. @user1804084 . Go through the git repository for better understanding.

            – Rohit Singh
            Jan 23 at 15:01







            I was trying to show a minimal code. getInstance() is how you instantiate a Fragment. But for you I have updated the code. @user1804084 . Go through the git repository for better understanding.

            – Rohit Singh
            Jan 23 at 15:01















            If you are wondering why I create instance this way. You can read this post stackoverflow.com/questions/9245408/…

            – Rohit Singh
            Jan 23 at 15:03







            If you are wondering why I create instance this way. You can read this post stackoverflow.com/questions/9245408/…

            – Rohit Singh
            Jan 23 at 15:03




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33655239%2fcommunicate-from-activity-to-fragment-using-interface%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith