How to set startActivityForResult from an adapter and get the result in a viewPager fragment
up vote
0
down vote
favorite
I know there are several related topics but I have tried them all and nothing happens. I have a "MainActivity" class that contains a fragment called "HomeFragment" which this fragment contains a viewPager with two fragments one of them called "ListFragment". By setting up an adapter named as "ListAdapter" an onClick method for each items works fine, the problem is when I set:
intentRequestDetails.putExtra(KEY_POSITION, position);
intentRequestDetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
((Activity) context).startActivityForResult(intentRequestDetails, 2);
to onItemClick in adapter,
I do not know how to get result in "ListFragment".
I pass "getActivity()" to adapter and ListFragment but non of them worked!
Please help me with this to get result in "ListFragment"
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
if (resultCode == Activity.RESULT_OK) {
int position = data.getIntExtra("position", -1);
userRequestsViewModel.getResult().remove(position);
viewHolder.rView.getAdapter().notifyItemRemoved(position);
viewHolder.rView.getAdapter().notifyItemRangeChanged(position, viewHolder.rView.getAdapter().getItemCount());
} else if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
What I need
is what to pass from fragment to adapter as context? where and how to override the onActivityResult and if there is anything else that i missed?
- The onActivityResult in ListFragment does not triggered since the startActivityForResult is being called from ListAdapter! How should I handle this?
- How to get adapter and ListFragment in MainActivity if its a chance to update the list?
android android-fragments android-adapter startactivityforresult
add a comment |
up vote
0
down vote
favorite
I know there are several related topics but I have tried them all and nothing happens. I have a "MainActivity" class that contains a fragment called "HomeFragment" which this fragment contains a viewPager with two fragments one of them called "ListFragment". By setting up an adapter named as "ListAdapter" an onClick method for each items works fine, the problem is when I set:
intentRequestDetails.putExtra(KEY_POSITION, position);
intentRequestDetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
((Activity) context).startActivityForResult(intentRequestDetails, 2);
to onItemClick in adapter,
I do not know how to get result in "ListFragment".
I pass "getActivity()" to adapter and ListFragment but non of them worked!
Please help me with this to get result in "ListFragment"
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
if (resultCode == Activity.RESULT_OK) {
int position = data.getIntExtra("position", -1);
userRequestsViewModel.getResult().remove(position);
viewHolder.rView.getAdapter().notifyItemRemoved(position);
viewHolder.rView.getAdapter().notifyItemRangeChanged(position, viewHolder.rView.getAdapter().getItemCount());
} else if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
What I need
is what to pass from fragment to adapter as context? where and how to override the onActivityResult and if there is anything else that i missed?
- The onActivityResult in ListFragment does not triggered since the startActivityForResult is being called from ListAdapter! How should I handle this?
- How to get adapter and ListFragment in MainActivity if its a chance to update the list?
android android-fragments android-adapter startactivityforresult
If you have overridden the methodonActivityResult
in your Activity, make sure you have not missed the call tosuper.onActivityResult
– Nabin Bhandari
Nov 11 at 12:23
No when I tried that again nothing happened
– seeya
Nov 11 at 12:31
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I know there are several related topics but I have tried them all and nothing happens. I have a "MainActivity" class that contains a fragment called "HomeFragment" which this fragment contains a viewPager with two fragments one of them called "ListFragment". By setting up an adapter named as "ListAdapter" an onClick method for each items works fine, the problem is when I set:
intentRequestDetails.putExtra(KEY_POSITION, position);
intentRequestDetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
((Activity) context).startActivityForResult(intentRequestDetails, 2);
to onItemClick in adapter,
I do not know how to get result in "ListFragment".
I pass "getActivity()" to adapter and ListFragment but non of them worked!
Please help me with this to get result in "ListFragment"
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
if (resultCode == Activity.RESULT_OK) {
int position = data.getIntExtra("position", -1);
userRequestsViewModel.getResult().remove(position);
viewHolder.rView.getAdapter().notifyItemRemoved(position);
viewHolder.rView.getAdapter().notifyItemRangeChanged(position, viewHolder.rView.getAdapter().getItemCount());
} else if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
What I need
is what to pass from fragment to adapter as context? where and how to override the onActivityResult and if there is anything else that i missed?
- The onActivityResult in ListFragment does not triggered since the startActivityForResult is being called from ListAdapter! How should I handle this?
- How to get adapter and ListFragment in MainActivity if its a chance to update the list?
android android-fragments android-adapter startactivityforresult
I know there are several related topics but I have tried them all and nothing happens. I have a "MainActivity" class that contains a fragment called "HomeFragment" which this fragment contains a viewPager with two fragments one of them called "ListFragment". By setting up an adapter named as "ListAdapter" an onClick method for each items works fine, the problem is when I set:
intentRequestDetails.putExtra(KEY_POSITION, position);
intentRequestDetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
((Activity) context).startActivityForResult(intentRequestDetails, 2);
to onItemClick in adapter,
I do not know how to get result in "ListFragment".
I pass "getActivity()" to adapter and ListFragment but non of them worked!
Please help me with this to get result in "ListFragment"
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
if (resultCode == Activity.RESULT_OK) {
int position = data.getIntExtra("position", -1);
userRequestsViewModel.getResult().remove(position);
viewHolder.rView.getAdapter().notifyItemRemoved(position);
viewHolder.rView.getAdapter().notifyItemRangeChanged(position, viewHolder.rView.getAdapter().getItemCount());
} else if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
What I need
is what to pass from fragment to adapter as context? where and how to override the onActivityResult and if there is anything else that i missed?
- The onActivityResult in ListFragment does not triggered since the startActivityForResult is being called from ListAdapter! How should I handle this?
- How to get adapter and ListFragment in MainActivity if its a chance to update the list?
android android-fragments android-adapter startactivityforresult
android android-fragments android-adapter startactivityforresult
edited Nov 11 at 13:37
asked Nov 11 at 12:11
seeya
34
34
If you have overridden the methodonActivityResult
in your Activity, make sure you have not missed the call tosuper.onActivityResult
– Nabin Bhandari
Nov 11 at 12:23
No when I tried that again nothing happened
– seeya
Nov 11 at 12:31
add a comment |
If you have overridden the methodonActivityResult
in your Activity, make sure you have not missed the call tosuper.onActivityResult
– Nabin Bhandari
Nov 11 at 12:23
No when I tried that again nothing happened
– seeya
Nov 11 at 12:31
If you have overridden the method
onActivityResult
in your Activity, make sure you have not missed the call to super.onActivityResult
– Nabin Bhandari
Nov 11 at 12:23
If you have overridden the method
onActivityResult
in your Activity, make sure you have not missed the call to super.onActivityResult
– Nabin Bhandari
Nov 11 at 12:23
No when I tried that again nothing happened
– seeya
Nov 11 at 12:31
No when I tried that again nothing happened
– seeya
Nov 11 at 12:31
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
In addition to override onActivityResult
in fragment, you also need override it in activity class:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById("yourFragment");
fragment.onActivityResult(requestCode, resultCode, data);
}
and in fragment:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//do something
}
I did it but still does not work, I have a fragment with two child and a MainActivity, I tried all possibilities!
– seeya
Nov 11 at 13:02
add a comment |
up vote
0
down vote
accepted
After spending hours of research I finally solve this problem as follow:
As I have two nested fragments as child and parent in a one activity and I design to take action on each item click on the recyclerView items and going to another activity to get the result back and refreshing the list I added this code in MainActivity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().getFragments().get(1);
fragment.onActivityResult(requestCode, resultCode, data);
}
which select the targeted fragment (parentFragment) to pass data to, and then in that parentFragment I add this method:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getChildFragmentManager().getFragments().get(1);
fragment.onActivityResult(requestCode, resultCode, data);
}
to send onActivityResult to the child fragment. After all in the child fragment:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2){
if (resultCode == Activity.RESULT_OK) {
int position = data.getIntExtra("position", -1);
userRequestsViewModel.getResult().remove(position);
viewHolder.rView.getAdapter().notifyItemRemoved(position);
viewHolder.rView.getAdapter().notifyItemRangeChanged(position, viewHolder.rView.getAdapter().getItemCount());
}
}
}
to refresh the recyclerView adapter and notify it that an item has been removed! Another important point is that in adapter I just did this:
Intent intent = new Intent(view.getContext(), SecondActivity.class);
intent.putExtra(KEY_POSITION, position);
((Activity) context).startActivityForResult(intent, 2);
and remove this line of code:
intentRequestDetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
and of course I passed the context of activity from fragment to adapter: simply
getContext()
I hope this works for you too if you deal with such a problem one day
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
In addition to override onActivityResult
in fragment, you also need override it in activity class:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById("yourFragment");
fragment.onActivityResult(requestCode, resultCode, data);
}
and in fragment:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//do something
}
I did it but still does not work, I have a fragment with two child and a MainActivity, I tried all possibilities!
– seeya
Nov 11 at 13:02
add a comment |
up vote
0
down vote
In addition to override onActivityResult
in fragment, you also need override it in activity class:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById("yourFragment");
fragment.onActivityResult(requestCode, resultCode, data);
}
and in fragment:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//do something
}
I did it but still does not work, I have a fragment with two child and a MainActivity, I tried all possibilities!
– seeya
Nov 11 at 13:02
add a comment |
up vote
0
down vote
up vote
0
down vote
In addition to override onActivityResult
in fragment, you also need override it in activity class:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById("yourFragment");
fragment.onActivityResult(requestCode, resultCode, data);
}
and in fragment:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//do something
}
In addition to override onActivityResult
in fragment, you also need override it in activity class:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById("yourFragment");
fragment.onActivityResult(requestCode, resultCode, data);
}
and in fragment:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//do something
}
answered Nov 11 at 12:48
navylover
3,01421118
3,01421118
I did it but still does not work, I have a fragment with two child and a MainActivity, I tried all possibilities!
– seeya
Nov 11 at 13:02
add a comment |
I did it but still does not work, I have a fragment with two child and a MainActivity, I tried all possibilities!
– seeya
Nov 11 at 13:02
I did it but still does not work, I have a fragment with two child and a MainActivity, I tried all possibilities!
– seeya
Nov 11 at 13:02
I did it but still does not work, I have a fragment with two child and a MainActivity, I tried all possibilities!
– seeya
Nov 11 at 13:02
add a comment |
up vote
0
down vote
accepted
After spending hours of research I finally solve this problem as follow:
As I have two nested fragments as child and parent in a one activity and I design to take action on each item click on the recyclerView items and going to another activity to get the result back and refreshing the list I added this code in MainActivity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().getFragments().get(1);
fragment.onActivityResult(requestCode, resultCode, data);
}
which select the targeted fragment (parentFragment) to pass data to, and then in that parentFragment I add this method:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getChildFragmentManager().getFragments().get(1);
fragment.onActivityResult(requestCode, resultCode, data);
}
to send onActivityResult to the child fragment. After all in the child fragment:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2){
if (resultCode == Activity.RESULT_OK) {
int position = data.getIntExtra("position", -1);
userRequestsViewModel.getResult().remove(position);
viewHolder.rView.getAdapter().notifyItemRemoved(position);
viewHolder.rView.getAdapter().notifyItemRangeChanged(position, viewHolder.rView.getAdapter().getItemCount());
}
}
}
to refresh the recyclerView adapter and notify it that an item has been removed! Another important point is that in adapter I just did this:
Intent intent = new Intent(view.getContext(), SecondActivity.class);
intent.putExtra(KEY_POSITION, position);
((Activity) context).startActivityForResult(intent, 2);
and remove this line of code:
intentRequestDetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
and of course I passed the context of activity from fragment to adapter: simply
getContext()
I hope this works for you too if you deal with such a problem one day
add a comment |
up vote
0
down vote
accepted
After spending hours of research I finally solve this problem as follow:
As I have two nested fragments as child and parent in a one activity and I design to take action on each item click on the recyclerView items and going to another activity to get the result back and refreshing the list I added this code in MainActivity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().getFragments().get(1);
fragment.onActivityResult(requestCode, resultCode, data);
}
which select the targeted fragment (parentFragment) to pass data to, and then in that parentFragment I add this method:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getChildFragmentManager().getFragments().get(1);
fragment.onActivityResult(requestCode, resultCode, data);
}
to send onActivityResult to the child fragment. After all in the child fragment:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2){
if (resultCode == Activity.RESULT_OK) {
int position = data.getIntExtra("position", -1);
userRequestsViewModel.getResult().remove(position);
viewHolder.rView.getAdapter().notifyItemRemoved(position);
viewHolder.rView.getAdapter().notifyItemRangeChanged(position, viewHolder.rView.getAdapter().getItemCount());
}
}
}
to refresh the recyclerView adapter and notify it that an item has been removed! Another important point is that in adapter I just did this:
Intent intent = new Intent(view.getContext(), SecondActivity.class);
intent.putExtra(KEY_POSITION, position);
((Activity) context).startActivityForResult(intent, 2);
and remove this line of code:
intentRequestDetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
and of course I passed the context of activity from fragment to adapter: simply
getContext()
I hope this works for you too if you deal with such a problem one day
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
After spending hours of research I finally solve this problem as follow:
As I have two nested fragments as child and parent in a one activity and I design to take action on each item click on the recyclerView items and going to another activity to get the result back and refreshing the list I added this code in MainActivity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().getFragments().get(1);
fragment.onActivityResult(requestCode, resultCode, data);
}
which select the targeted fragment (parentFragment) to pass data to, and then in that parentFragment I add this method:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getChildFragmentManager().getFragments().get(1);
fragment.onActivityResult(requestCode, resultCode, data);
}
to send onActivityResult to the child fragment. After all in the child fragment:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2){
if (resultCode == Activity.RESULT_OK) {
int position = data.getIntExtra("position", -1);
userRequestsViewModel.getResult().remove(position);
viewHolder.rView.getAdapter().notifyItemRemoved(position);
viewHolder.rView.getAdapter().notifyItemRangeChanged(position, viewHolder.rView.getAdapter().getItemCount());
}
}
}
to refresh the recyclerView adapter and notify it that an item has been removed! Another important point is that in adapter I just did this:
Intent intent = new Intent(view.getContext(), SecondActivity.class);
intent.putExtra(KEY_POSITION, position);
((Activity) context).startActivityForResult(intent, 2);
and remove this line of code:
intentRequestDetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
and of course I passed the context of activity from fragment to adapter: simply
getContext()
I hope this works for you too if you deal with such a problem one day
After spending hours of research I finally solve this problem as follow:
As I have two nested fragments as child and parent in a one activity and I design to take action on each item click on the recyclerView items and going to another activity to get the result back and refreshing the list I added this code in MainActivity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().getFragments().get(1);
fragment.onActivityResult(requestCode, resultCode, data);
}
which select the targeted fragment (parentFragment) to pass data to, and then in that parentFragment I add this method:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getChildFragmentManager().getFragments().get(1);
fragment.onActivityResult(requestCode, resultCode, data);
}
to send onActivityResult to the child fragment. After all in the child fragment:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2){
if (resultCode == Activity.RESULT_OK) {
int position = data.getIntExtra("position", -1);
userRequestsViewModel.getResult().remove(position);
viewHolder.rView.getAdapter().notifyItemRemoved(position);
viewHolder.rView.getAdapter().notifyItemRangeChanged(position, viewHolder.rView.getAdapter().getItemCount());
}
}
}
to refresh the recyclerView adapter and notify it that an item has been removed! Another important point is that in adapter I just did this:
Intent intent = new Intent(view.getContext(), SecondActivity.class);
intent.putExtra(KEY_POSITION, position);
((Activity) context).startActivityForResult(intent, 2);
and remove this line of code:
intentRequestDetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
and of course I passed the context of activity from fragment to adapter: simply
getContext()
I hope this works for you too if you deal with such a problem one day
edited Nov 11 at 18:12
answered Nov 11 at 18:03
seeya
34
34
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53248617%2fhow-to-set-startactivityforresult-from-an-adapter-and-get-the-result-in-a-viewpa%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
If you have overridden the method
onActivityResult
in your Activity, make sure you have not missed the call tosuper.onActivityResult
– Nabin Bhandari
Nov 11 at 12:23
No when I tried that again nothing happened
– seeya
Nov 11 at 12:31