Initialize button in one fragment but set click on another fragment












-2















I have defined the button in FragmentA but I want to set its click on FragmentB. I have used interfaces for that but it causes null pointer exception. Please suggest me some way.










share|improve this question























  • They'll need to have the same parent activity and then just have the click method you want to call in the activity. Calling one fragment from another is a very bad practice.

    – Sub 6 Resources
    Nov 22 '18 at 5:11











  • I have two tab layouts into one another so calling fragment from fragment is only choice for me

    – Divyank Vijayvergiya
    Nov 22 '18 at 5:15











  • Please post the code you have so far.

    – Sub 6 Resources
    Nov 22 '18 at 5:16











  • stackoverflow.com/help/how-to-ask

    – dominicoder
    Nov 22 '18 at 5:41











  • Don't do that. Don't try to manipulate one fragment from another fragment. At least use activity for that.

    – Vladyslav Matviienko
    Nov 22 '18 at 6:25
















-2















I have defined the button in FragmentA but I want to set its click on FragmentB. I have used interfaces for that but it causes null pointer exception. Please suggest me some way.










share|improve this question























  • They'll need to have the same parent activity and then just have the click method you want to call in the activity. Calling one fragment from another is a very bad practice.

    – Sub 6 Resources
    Nov 22 '18 at 5:11











  • I have two tab layouts into one another so calling fragment from fragment is only choice for me

    – Divyank Vijayvergiya
    Nov 22 '18 at 5:15











  • Please post the code you have so far.

    – Sub 6 Resources
    Nov 22 '18 at 5:16











  • stackoverflow.com/help/how-to-ask

    – dominicoder
    Nov 22 '18 at 5:41











  • Don't do that. Don't try to manipulate one fragment from another fragment. At least use activity for that.

    – Vladyslav Matviienko
    Nov 22 '18 at 6:25














-2












-2








-2








I have defined the button in FragmentA but I want to set its click on FragmentB. I have used interfaces for that but it causes null pointer exception. Please suggest me some way.










share|improve this question














I have defined the button in FragmentA but I want to set its click on FragmentB. I have used interfaces for that but it causes null pointer exception. Please suggest me some way.







android android-fragments






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 5:08









Divyank VijayvergiyaDivyank Vijayvergiya

8613




8613













  • They'll need to have the same parent activity and then just have the click method you want to call in the activity. Calling one fragment from another is a very bad practice.

    – Sub 6 Resources
    Nov 22 '18 at 5:11











  • I have two tab layouts into one another so calling fragment from fragment is only choice for me

    – Divyank Vijayvergiya
    Nov 22 '18 at 5:15











  • Please post the code you have so far.

    – Sub 6 Resources
    Nov 22 '18 at 5:16











  • stackoverflow.com/help/how-to-ask

    – dominicoder
    Nov 22 '18 at 5:41











  • Don't do that. Don't try to manipulate one fragment from another fragment. At least use activity for that.

    – Vladyslav Matviienko
    Nov 22 '18 at 6:25



















  • They'll need to have the same parent activity and then just have the click method you want to call in the activity. Calling one fragment from another is a very bad practice.

    – Sub 6 Resources
    Nov 22 '18 at 5:11











  • I have two tab layouts into one another so calling fragment from fragment is only choice for me

    – Divyank Vijayvergiya
    Nov 22 '18 at 5:15











  • Please post the code you have so far.

    – Sub 6 Resources
    Nov 22 '18 at 5:16











  • stackoverflow.com/help/how-to-ask

    – dominicoder
    Nov 22 '18 at 5:41











  • Don't do that. Don't try to manipulate one fragment from another fragment. At least use activity for that.

    – Vladyslav Matviienko
    Nov 22 '18 at 6:25

















They'll need to have the same parent activity and then just have the click method you want to call in the activity. Calling one fragment from another is a very bad practice.

– Sub 6 Resources
Nov 22 '18 at 5:11





They'll need to have the same parent activity and then just have the click method you want to call in the activity. Calling one fragment from another is a very bad practice.

– Sub 6 Resources
Nov 22 '18 at 5:11













I have two tab layouts into one another so calling fragment from fragment is only choice for me

– Divyank Vijayvergiya
Nov 22 '18 at 5:15





I have two tab layouts into one another so calling fragment from fragment is only choice for me

– Divyank Vijayvergiya
Nov 22 '18 at 5:15













Please post the code you have so far.

– Sub 6 Resources
Nov 22 '18 at 5:16





Please post the code you have so far.

– Sub 6 Resources
Nov 22 '18 at 5:16













stackoverflow.com/help/how-to-ask

– dominicoder
Nov 22 '18 at 5:41





stackoverflow.com/help/how-to-ask

– dominicoder
Nov 22 '18 at 5:41













Don't do that. Don't try to manipulate one fragment from another fragment. At least use activity for that.

– Vladyslav Matviienko
Nov 22 '18 at 6:25





Don't do that. Don't try to manipulate one fragment from another fragment. At least use activity for that.

– Vladyslav Matviienko
Nov 22 '18 at 6:25












1 Answer
1






active

oldest

votes


















1














This is very common in Android and can be called inter component communication, where you can trigger event or message from one component to another component.



Though there are many ways to achieve it like using static entities, in your case it will be coded at parent activity because both fragment case see it, but it is not good practice and have many side effects.



You may consider to use event handler specific libraries such as EventBus, Otto, RxJava and RxAndroid.



As a beginner you may start with EventBus, though personally I like to use Rxjava.






share|improve this answer

























    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%2f53424232%2finitialize-button-in-one-fragment-but-set-click-on-another-fragment%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    This is very common in Android and can be called inter component communication, where you can trigger event or message from one component to another component.



    Though there are many ways to achieve it like using static entities, in your case it will be coded at parent activity because both fragment case see it, but it is not good practice and have many side effects.



    You may consider to use event handler specific libraries such as EventBus, Otto, RxJava and RxAndroid.



    As a beginner you may start with EventBus, though personally I like to use Rxjava.






    share|improve this answer






























      1














      This is very common in Android and can be called inter component communication, where you can trigger event or message from one component to another component.



      Though there are many ways to achieve it like using static entities, in your case it will be coded at parent activity because both fragment case see it, but it is not good practice and have many side effects.



      You may consider to use event handler specific libraries such as EventBus, Otto, RxJava and RxAndroid.



      As a beginner you may start with EventBus, though personally I like to use Rxjava.






      share|improve this answer




























        1












        1








        1







        This is very common in Android and can be called inter component communication, where you can trigger event or message from one component to another component.



        Though there are many ways to achieve it like using static entities, in your case it will be coded at parent activity because both fragment case see it, but it is not good practice and have many side effects.



        You may consider to use event handler specific libraries such as EventBus, Otto, RxJava and RxAndroid.



        As a beginner you may start with EventBus, though personally I like to use Rxjava.






        share|improve this answer















        This is very common in Android and can be called inter component communication, where you can trigger event or message from one component to another component.



        Though there are many ways to achieve it like using static entities, in your case it will be coded at parent activity because both fragment case see it, but it is not good practice and have many side effects.



        You may consider to use event handler specific libraries such as EventBus, Otto, RxJava and RxAndroid.



        As a beginner you may start with EventBus, though personally I like to use Rxjava.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 5:25

























        answered Nov 22 '18 at 5:20







        user7154703







































            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%2f53424232%2finitialize-button-in-one-fragment-but-set-click-on-another-fragment%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

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]