Is it possible to pass a value from Adapter to Activity?

Pavan :

I have Activity1, Activity1 Adapter and Activity2

I'm not able to pass value between an Adapter and Activity. When back button is pressed, I'm expecting a value to be coming from the Second Activity (Activity 2) to Activity1 Currently, it gives me null

Here are my code snippets.

Activity1 Adapter

 holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity origin = (Activity) context;
                Intent intent = new Intent(context, PostActivity.class);
                intent.putExtra("searchText", staggeredCustomCard.getSearchText());
                origin.startActivityForResult(intent, 1);
            }
        });

Activity2

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent mIntent = new Intent();
        mIntent.getStringExtra("searchText");
        setResult(1, mIntent);
    }

I'm expecting this searchText to be going to Activity1. Could anyone please guide me how to achieve this?

Bach Vu :

You are getting getStringExtra("searchText"); from a completely new intent, that's why it's returning null. You need to get search text from getIntent() like this:

@Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent mIntent = new Intent();
        String search = getIntent().getStringExtra("searchText");
        mIntent.putExtra("searchText", search);
        setResult(1, mIntent);
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=306297&siteId=1