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?









share|improve this question























  • 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














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?









share|improve this question























  • 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












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?









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 13:37

























asked Nov 11 at 12:11









seeya

34




34











  • 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
















  • 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















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












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






share|improve this answer




















  • 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

















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






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%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

























    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






    share|improve this answer




















    • 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














    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






    share|improve this answer




















    • 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












    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






    share|improve this answer












    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







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 11 at 12:48









    navylover

    3,03421118




    3,03421118











    • 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




    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












    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






    share|improve this answer


























      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






      share|improve this answer
























        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






        share|improve this answer














        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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 18:12

























        answered Nov 11 at 18:03









        seeya

        34




        34



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto