return value to fragment from activity android

前端 未结 1 1511
难免孤独
难免孤独 2021-01-27 00:40

I have Fragment(F) which calls the activity(A). On press of button(B) in activity(A) the activity must return selected arraylist value back to fragment(F) and finish activity(A)

1条回答
  •  清酒与你
    2021-01-27 01:20

    In your fragment(F) call the startActivityForResult() method to start the activity A, then override onActivityResult() method in the fragment.

    So when the activity A finishes you can get get the result bundle in onActivityResult() method.

    EDIT

    In your fragment onClick() call the activity like this

    Intent myIintent=new Intent(getActivity(), AndroidCustomGalleryActivity.class);
        startActivityForResult(myIintent, 999);
    

    And in your activity

    Intent intent = getIntent();
        Bundle bundle = new Bundle();
        bundle.putString("key", "value");
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);
        finish();
    

    Now you will get the result in onActivityResult() method of the fragment.

    EDIT2

    In your fragment onActivityResult() method you have to get the data like this...

    Bundle extras = data.getExtras();
        strtext =   extras.getString("key");
    

    0 讨论(0)
提交回复
热议问题