Initialize button in one fragment but set click on another fragment
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
add a comment |
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
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
add a comment |
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
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
android android-fragments
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 22 '18 at 5:25
answered Nov 22 '18 at 5:20
user7154703
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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