Communicate from Activity to Fragment using Interface
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.
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



|
show 3 more comments
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.
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



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
|
show 3 more comments
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.
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



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.
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



java



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
|
show 3 more comments
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
|
show 3 more comments
4 Answers
4
active
oldest
votes
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.
1
Yes...did exactly that way. Thanks @Daniel Nugent
– Swapnil
Nov 12 '15 at 8:11
add a comment |
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. **/
}
}
...
}`
1
This is Fragment -> Activity, not Activity -> Fragment
– Linxy
Jan 15 '17 at 20:21
add a comment |
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)'
add a comment |
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
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
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%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
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.
1
Yes...did exactly that way. Thanks @Daniel Nugent
– Swapnil
Nov 12 '15 at 8:11
add a comment |
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.
1
Yes...did exactly that way. Thanks @Daniel Nugent
– Swapnil
Nov 12 '15 at 8:11
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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. **/
}
}
...
}`
1
This is Fragment -> Activity, not Activity -> Fragment
– Linxy
Jan 15 '17 at 20:21
add a comment |
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. **/
}
}
...
}`
1
This is Fragment -> Activity, not Activity -> Fragment
– Linxy
Jan 15 '17 at 20:21
add a comment |
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. **/
}
}
...
}`
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. **/
}
}
...
}`
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
add a comment |
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
add a comment |
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)'
add a comment |
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)'
add a comment |
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)'
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)'
answered Nov 15 '18 at 13:54


AnubhavAnubhav
46968
46968
add a comment |
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
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%2f33655239%2fcommunicate-from-activity-to-fragment-using-interface%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
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