0

Is it possible to setArguments() on onActivityResult() and pass it to FragmentActivity?

From my SecondActivity, I am passing a bundle to my FirstActivity through onBackPressed().

  @Override
    public void onBackPressed() {

        Bundle bundle = new Bundle();
        bundle.putString("SORT_VALUE", SORT_BY_VALUE);
        bundle.putString("SORT_BY", sortby);
        bundle.putString("FROM_SORT", "FROM_SORT");
        Intent intent = new Intent();
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);
        back();
        super.onBackPressed();
    }

I have this code onActivityResult().

if (requestCode == SORT_RESULT) {
                if (data != null && data.getExtras() != null) {
                    Bundle bundle = data.getExtras();
                    SORT_VALUE = bundle.getString("SORT_VALUE");
                    sortby = bundle.getString("SORT_BY");
                    FILTER_VALUE = bundle.getString("FILTER_VALUE");
                    filterby = bundle.getString("FILTER_BY");


                    final ProductListFragment frg = new ProductListFragment();;

                    Bundle extra = new Bundle();
                    extra.putString("SORT_VALUE", SORT_VALUE);
                    extra.putString("SORT_BY", sortby);
                    frg.setArguments(extra);

                    if (SORT_VALUE != null) {
                        if (SORT_VALUE.equalsIgnoreCase("Popularity")) {
                            setSortValueAndSortBy("Popularity", "fmcg-campaign.status=active,-monthly-popular-score");
                        }

//this contains code for other options
            }

This is my code for the FragmentActivity, it calls the method, onCreate() but values return null on my end.

  @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle args = getArguments();
        SORT_VALUE = args.getString("SORT_VALUE");
        SORT_BY = args.getString("SORT_BY");

    }
rereh
  • 1
  • 3
  • 1
    How many activities are you trying to pass this data in, you mentioned a SecondActivity, a FirstActivity and a FragmentActivity. Are last two supposed to be the same. Or are you trying to pass this value into ```ProductListFragment``` in that case you should fetch the value inside the fragment not the activity. For one, Activity's ```onCreate``` doesn't get called if its already created, and secondly bundles passed into a fragment should be called within a fragment not its activity – ljk Mar 09 '20 at 09:13

1 Answers1

0

what is it?

SORT_VALUE = args.getString("SORT_VALUE");
SORT_BY = args.getString("SORT_BY");

you trying to write string in your constants?
try something like

String sortValue = args.getString("SORT_VALUE");

read more about bundle usage here
but usually bundle need for passing data between fragment
for activities use

Intent intent = new Intent(getBaseContext(), NewActivity.class);
intent.putExtra("YOUR_CONSTANT", yourData);
startActivity(intent);

Access that intent on next activity:

String sessionId = getIntent().getStringExtra("YOUR_CONSTANT");
Kirguduck
  • 748
  • 1
  • 9
  • 20