How do I pass callback function from one fragment inside an Activity to second activity?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a callback interface.
public interface RowCallback {
void callOnRowAction(String action);
}
My fragment FragmentA is attached to ActivityA in which FragmentA is defined as follows:
class FragmentA extends Fragment implements RowCallback {
@Override
void callOnRowAction(String action) {
. . . Custom implementation . . .
}
... Other methods . . .
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
mButton = view.findViewById(R.id.my_button);
mButton.setOnClickListener((view) -> {
// I want to pass the callback instance (which is FragmentA) to activity B.
Intent intent = new Intent(getActivity(), ActivityB.class);
. . . Some more intent initialisation. . .
startActivity(intent);
});
return view;
}
}
ActivityB source Code
public class ActivityB extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = createFragment(getIntent());
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
}
}
private Fragment createFragment(Intent intent) {
FragmentB fragment = new FragmentB();
Bundle bundle = new Bundle();
. . . . initialise bundle . . .
// I want to pass the callback instance from the intent to the callback function.
// In this example, the callback instance would be an instance of fragment A.
fragment.setArguments(bundle);
return fragment;
}
}
In an instance of FragmentB, I want to invoke a call back method should a logical condition be satisfied.I want to return to ActivityA/FragmentA after the callback function has been invoked by calling getActivity().finish();
How can I achieve something like this?
add a comment |
I have a callback interface.
public interface RowCallback {
void callOnRowAction(String action);
}
My fragment FragmentA is attached to ActivityA in which FragmentA is defined as follows:
class FragmentA extends Fragment implements RowCallback {
@Override
void callOnRowAction(String action) {
. . . Custom implementation . . .
}
... Other methods . . .
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
mButton = view.findViewById(R.id.my_button);
mButton.setOnClickListener((view) -> {
// I want to pass the callback instance (which is FragmentA) to activity B.
Intent intent = new Intent(getActivity(), ActivityB.class);
. . . Some more intent initialisation. . .
startActivity(intent);
});
return view;
}
}
ActivityB source Code
public class ActivityB extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = createFragment(getIntent());
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
}
}
private Fragment createFragment(Intent intent) {
FragmentB fragment = new FragmentB();
Bundle bundle = new Bundle();
. . . . initialise bundle . . .
// I want to pass the callback instance from the intent to the callback function.
// In this example, the callback instance would be an instance of fragment A.
fragment.setArguments(bundle);
return fragment;
}
}
In an instance of FragmentB, I want to invoke a call back method should a logical condition be satisfied.I want to return to ActivityA/FragmentA after the callback function has been invoked by calling getActivity().finish();
How can I achieve something like this?
add a comment |
I have a callback interface.
public interface RowCallback {
void callOnRowAction(String action);
}
My fragment FragmentA is attached to ActivityA in which FragmentA is defined as follows:
class FragmentA extends Fragment implements RowCallback {
@Override
void callOnRowAction(String action) {
. . . Custom implementation . . .
}
... Other methods . . .
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
mButton = view.findViewById(R.id.my_button);
mButton.setOnClickListener((view) -> {
// I want to pass the callback instance (which is FragmentA) to activity B.
Intent intent = new Intent(getActivity(), ActivityB.class);
. . . Some more intent initialisation. . .
startActivity(intent);
});
return view;
}
}
ActivityB source Code
public class ActivityB extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = createFragment(getIntent());
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
}
}
private Fragment createFragment(Intent intent) {
FragmentB fragment = new FragmentB();
Bundle bundle = new Bundle();
. . . . initialise bundle . . .
// I want to pass the callback instance from the intent to the callback function.
// In this example, the callback instance would be an instance of fragment A.
fragment.setArguments(bundle);
return fragment;
}
}
In an instance of FragmentB, I want to invoke a call back method should a logical condition be satisfied.I want to return to ActivityA/FragmentA after the callback function has been invoked by calling getActivity().finish();
How can I achieve something like this?
I have a callback interface.
public interface RowCallback {
void callOnRowAction(String action);
}
My fragment FragmentA is attached to ActivityA in which FragmentA is defined as follows:
class FragmentA extends Fragment implements RowCallback {
@Override
void callOnRowAction(String action) {
. . . Custom implementation . . .
}
... Other methods . . .
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
mButton = view.findViewById(R.id.my_button);
mButton.setOnClickListener((view) -> {
// I want to pass the callback instance (which is FragmentA) to activity B.
Intent intent = new Intent(getActivity(), ActivityB.class);
. . . Some more intent initialisation. . .
startActivity(intent);
});
return view;
}
}
ActivityB source Code
public class ActivityB extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = createFragment(getIntent());
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
}
}
private Fragment createFragment(Intent intent) {
FragmentB fragment = new FragmentB();
Bundle bundle = new Bundle();
. . . . initialise bundle . . .
// I want to pass the callback instance from the intent to the callback function.
// In this example, the callback instance would be an instance of fragment A.
fragment.setArguments(bundle);
return fragment;
}
}
In an instance of FragmentB, I want to invoke a call back method should a logical condition be satisfied.I want to return to ActivityA/FragmentA after the callback function has been invoked by calling getActivity().finish();
How can I achieve something like this?
edited Nov 24 '18 at 13:10
manoflogan
asked Nov 24 '18 at 11:31
manofloganmanoflogan
65
65
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
I will split my answer:
If you want a callback for one time:
- Fragment A
OnclickListener
will call to his Activity (lets say A). - Activity A will do
StartActivityForResult(intent)to Activity B. - Activity B will
attach Fragment B to himself. - Fragment B
OnclickListenerwill
update his Activity (B). - Activity B will return IntentResult and Activity A will catch it on
onActivityResult.
- Fragment A
If you want a callback for multiple times:
- You can use the architecture of answer 1 - with Activity communication using
LocalBroadcastManager
An example to Activity communication using LocalBroadcastManager
- You can use the architecture of answer 1 - with Activity communication using
Actually FragmentA's (which is created by ActivityA) button's on click listener will call Activity B from within the fragment. All the logic is encapsulated within the fragment. Activity does not do anything other than instantiating the fragment. I want the callback to be invoked every time a logical condition inside FragmentB is satisfied. For example, if I had a reference to the callback instance in Fragment B, I would simply callcallbackInstance.callOnRowAction("delete");within FragmentB's methods.
– manoflogan
Nov 24 '18 at 11:59
I also want to return back to ActivityA after the task is done.
– manoflogan
Nov 24 '18 at 12:21
can you show me a code sample on how to do it?
– manoflogan
Nov 25 '18 at 3:28
add a comment |
Implement function in within fragment which you want get call back after execute
public void testCallBack(Callback callback) {
callback.callOnRowAction("callback");
}
Implement this in Activity class
public void getCallBack() {
FragmentA.newInstance().testCallBack(new FragmentA.Callback() {
@Override
public void callOnRowAction(String action) {
//you can do something
}
});
}
if you want to excute testCallBack method when the function start you can call this inside onCreate method
How do I load ActivityB from a click event of a button that has been initliased in FragmentA of ActivityA? I want to go back ActivityA after the callback has been invoked. So I will be calling getActivity().finish() after the callback function has been called in FragmentB's method.
– manoflogan
Nov 24 '18 at 12:27
add a comment |
You could inside the fragment add
if(getParentFragment() instanceof Activity1)
((Activity1) getParentFragment()).setRowCallback(this);
else if(getParentFragment() instanceof Activity2)
((Activity2) getParentFragment()).setRowCallback(this);
the fragment will automatically call the callback of his parent activity
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%2f53457681%2fhow-do-i-pass-callback-function-from-one-fragment-inside-an-activity-to-second-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I will split my answer:
If you want a callback for one time:
- Fragment A
OnclickListener
will call to his Activity (lets say A). - Activity A will do
StartActivityForResult(intent)to Activity B. - Activity B will
attach Fragment B to himself. - Fragment B
OnclickListenerwill
update his Activity (B). - Activity B will return IntentResult and Activity A will catch it on
onActivityResult.
- Fragment A
If you want a callback for multiple times:
- You can use the architecture of answer 1 - with Activity communication using
LocalBroadcastManager
An example to Activity communication using LocalBroadcastManager
- You can use the architecture of answer 1 - with Activity communication using
Actually FragmentA's (which is created by ActivityA) button's on click listener will call Activity B from within the fragment. All the logic is encapsulated within the fragment. Activity does not do anything other than instantiating the fragment. I want the callback to be invoked every time a logical condition inside FragmentB is satisfied. For example, if I had a reference to the callback instance in Fragment B, I would simply callcallbackInstance.callOnRowAction("delete");within FragmentB's methods.
– manoflogan
Nov 24 '18 at 11:59
I also want to return back to ActivityA after the task is done.
– manoflogan
Nov 24 '18 at 12:21
can you show me a code sample on how to do it?
– manoflogan
Nov 25 '18 at 3:28
add a comment |
I will split my answer:
If you want a callback for one time:
- Fragment A
OnclickListener
will call to his Activity (lets say A). - Activity A will do
StartActivityForResult(intent)to Activity B. - Activity B will
attach Fragment B to himself. - Fragment B
OnclickListenerwill
update his Activity (B). - Activity B will return IntentResult and Activity A will catch it on
onActivityResult.
- Fragment A
If you want a callback for multiple times:
- You can use the architecture of answer 1 - with Activity communication using
LocalBroadcastManager
An example to Activity communication using LocalBroadcastManager
- You can use the architecture of answer 1 - with Activity communication using
Actually FragmentA's (which is created by ActivityA) button's on click listener will call Activity B from within the fragment. All the logic is encapsulated within the fragment. Activity does not do anything other than instantiating the fragment. I want the callback to be invoked every time a logical condition inside FragmentB is satisfied. For example, if I had a reference to the callback instance in Fragment B, I would simply callcallbackInstance.callOnRowAction("delete");within FragmentB's methods.
– manoflogan
Nov 24 '18 at 11:59
I also want to return back to ActivityA after the task is done.
– manoflogan
Nov 24 '18 at 12:21
can you show me a code sample on how to do it?
– manoflogan
Nov 25 '18 at 3:28
add a comment |
I will split my answer:
If you want a callback for one time:
- Fragment A
OnclickListener
will call to his Activity (lets say A). - Activity A will do
StartActivityForResult(intent)to Activity B. - Activity B will
attach Fragment B to himself. - Fragment B
OnclickListenerwill
update his Activity (B). - Activity B will return IntentResult and Activity A will catch it on
onActivityResult.
- Fragment A
If you want a callback for multiple times:
- You can use the architecture of answer 1 - with Activity communication using
LocalBroadcastManager
An example to Activity communication using LocalBroadcastManager
- You can use the architecture of answer 1 - with Activity communication using
I will split my answer:
If you want a callback for one time:
- Fragment A
OnclickListener
will call to his Activity (lets say A). - Activity A will do
StartActivityForResult(intent)to Activity B. - Activity B will
attach Fragment B to himself. - Fragment B
OnclickListenerwill
update his Activity (B). - Activity B will return IntentResult and Activity A will catch it on
onActivityResult.
- Fragment A
If you want a callback for multiple times:
- You can use the architecture of answer 1 - with Activity communication using
LocalBroadcastManager
An example to Activity communication using LocalBroadcastManager
- You can use the architecture of answer 1 - with Activity communication using
answered Nov 24 '18 at 11:52
motis10motis10
1,44811130
1,44811130
Actually FragmentA's (which is created by ActivityA) button's on click listener will call Activity B from within the fragment. All the logic is encapsulated within the fragment. Activity does not do anything other than instantiating the fragment. I want the callback to be invoked every time a logical condition inside FragmentB is satisfied. For example, if I had a reference to the callback instance in Fragment B, I would simply callcallbackInstance.callOnRowAction("delete");within FragmentB's methods.
– manoflogan
Nov 24 '18 at 11:59
I also want to return back to ActivityA after the task is done.
– manoflogan
Nov 24 '18 at 12:21
can you show me a code sample on how to do it?
– manoflogan
Nov 25 '18 at 3:28
add a comment |
Actually FragmentA's (which is created by ActivityA) button's on click listener will call Activity B from within the fragment. All the logic is encapsulated within the fragment. Activity does not do anything other than instantiating the fragment. I want the callback to be invoked every time a logical condition inside FragmentB is satisfied. For example, if I had a reference to the callback instance in Fragment B, I would simply callcallbackInstance.callOnRowAction("delete");within FragmentB's methods.
– manoflogan
Nov 24 '18 at 11:59
I also want to return back to ActivityA after the task is done.
– manoflogan
Nov 24 '18 at 12:21
can you show me a code sample on how to do it?
– manoflogan
Nov 25 '18 at 3:28
Actually FragmentA's (which is created by ActivityA) button's on click listener will call Activity B from within the fragment. All the logic is encapsulated within the fragment. Activity does not do anything other than instantiating the fragment. I want the callback to be invoked every time a logical condition inside FragmentB is satisfied. For example, if I had a reference to the callback instance in Fragment B, I would simply call
callbackInstance.callOnRowAction("delete"); within FragmentB's methods.– manoflogan
Nov 24 '18 at 11:59
Actually FragmentA's (which is created by ActivityA) button's on click listener will call Activity B from within the fragment. All the logic is encapsulated within the fragment. Activity does not do anything other than instantiating the fragment. I want the callback to be invoked every time a logical condition inside FragmentB is satisfied. For example, if I had a reference to the callback instance in Fragment B, I would simply call
callbackInstance.callOnRowAction("delete"); within FragmentB's methods.– manoflogan
Nov 24 '18 at 11:59
I also want to return back to ActivityA after the task is done.
– manoflogan
Nov 24 '18 at 12:21
I also want to return back to ActivityA after the task is done.
– manoflogan
Nov 24 '18 at 12:21
can you show me a code sample on how to do it?
– manoflogan
Nov 25 '18 at 3:28
can you show me a code sample on how to do it?
– manoflogan
Nov 25 '18 at 3:28
add a comment |
Implement function in within fragment which you want get call back after execute
public void testCallBack(Callback callback) {
callback.callOnRowAction("callback");
}
Implement this in Activity class
public void getCallBack() {
FragmentA.newInstance().testCallBack(new FragmentA.Callback() {
@Override
public void callOnRowAction(String action) {
//you can do something
}
});
}
if you want to excute testCallBack method when the function start you can call this inside onCreate method
How do I load ActivityB from a click event of a button that has been initliased in FragmentA of ActivityA? I want to go back ActivityA after the callback has been invoked. So I will be calling getActivity().finish() after the callback function has been called in FragmentB's method.
– manoflogan
Nov 24 '18 at 12:27
add a comment |
Implement function in within fragment which you want get call back after execute
public void testCallBack(Callback callback) {
callback.callOnRowAction("callback");
}
Implement this in Activity class
public void getCallBack() {
FragmentA.newInstance().testCallBack(new FragmentA.Callback() {
@Override
public void callOnRowAction(String action) {
//you can do something
}
});
}
if you want to excute testCallBack method when the function start you can call this inside onCreate method
How do I load ActivityB from a click event of a button that has been initliased in FragmentA of ActivityA? I want to go back ActivityA after the callback has been invoked. So I will be calling getActivity().finish() after the callback function has been called in FragmentB's method.
– manoflogan
Nov 24 '18 at 12:27
add a comment |
Implement function in within fragment which you want get call back after execute
public void testCallBack(Callback callback) {
callback.callOnRowAction("callback");
}
Implement this in Activity class
public void getCallBack() {
FragmentA.newInstance().testCallBack(new FragmentA.Callback() {
@Override
public void callOnRowAction(String action) {
//you can do something
}
});
}
if you want to excute testCallBack method when the function start you can call this inside onCreate method
Implement function in within fragment which you want get call back after execute
public void testCallBack(Callback callback) {
callback.callOnRowAction("callback");
}
Implement this in Activity class
public void getCallBack() {
FragmentA.newInstance().testCallBack(new FragmentA.Callback() {
@Override
public void callOnRowAction(String action) {
//you can do something
}
});
}
if you want to excute testCallBack method when the function start you can call this inside onCreate method
answered Nov 24 '18 at 12:20
madu_devmadu_dev
593
593
How do I load ActivityB from a click event of a button that has been initliased in FragmentA of ActivityA? I want to go back ActivityA after the callback has been invoked. So I will be calling getActivity().finish() after the callback function has been called in FragmentB's method.
– manoflogan
Nov 24 '18 at 12:27
add a comment |
How do I load ActivityB from a click event of a button that has been initliased in FragmentA of ActivityA? I want to go back ActivityA after the callback has been invoked. So I will be calling getActivity().finish() after the callback function has been called in FragmentB's method.
– manoflogan
Nov 24 '18 at 12:27
How do I load ActivityB from a click event of a button that has been initliased in FragmentA of ActivityA? I want to go back ActivityA after the callback has been invoked. So I will be calling getActivity().finish() after the callback function has been called in FragmentB's method.
– manoflogan
Nov 24 '18 at 12:27
How do I load ActivityB from a click event of a button that has been initliased in FragmentA of ActivityA? I want to go back ActivityA after the callback has been invoked. So I will be calling getActivity().finish() after the callback function has been called in FragmentB's method.
– manoflogan
Nov 24 '18 at 12:27
add a comment |
You could inside the fragment add
if(getParentFragment() instanceof Activity1)
((Activity1) getParentFragment()).setRowCallback(this);
else if(getParentFragment() instanceof Activity2)
((Activity2) getParentFragment()).setRowCallback(this);
the fragment will automatically call the callback of his parent activity
add a comment |
You could inside the fragment add
if(getParentFragment() instanceof Activity1)
((Activity1) getParentFragment()).setRowCallback(this);
else if(getParentFragment() instanceof Activity2)
((Activity2) getParentFragment()).setRowCallback(this);
the fragment will automatically call the callback of his parent activity
add a comment |
You could inside the fragment add
if(getParentFragment() instanceof Activity1)
((Activity1) getParentFragment()).setRowCallback(this);
else if(getParentFragment() instanceof Activity2)
((Activity2) getParentFragment()).setRowCallback(this);
the fragment will automatically call the callback of his parent activity
You could inside the fragment add
if(getParentFragment() instanceof Activity1)
((Activity1) getParentFragment()).setRowCallback(this);
else if(getParentFragment() instanceof Activity2)
((Activity2) getParentFragment()).setRowCallback(this);
the fragment will automatically call the callback of his parent activity
answered Nov 24 '18 at 13:59
Master FathiMaster Fathi
3531817
3531817
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%2f53457681%2fhow-do-i-pass-callback-function-from-one-fragment-inside-an-activity-to-second-a%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