-3

I am making a simple online book store app. The two activities I want to share information between are activity A which is a recyclerView filled with books and activity B which displays more information about a book when said book is clicked in activity A. I want to add some functionality where when you click on the author name of a book in activity B, the application will send the authors name back to activity A and filter the books in such a way that only books from the chosen author are showed.

Filtering the data is an easy task to solve. However I do not know how to get the author name back to activity A. Is using an intent the right call here? Will the intent not start an entirely new A activity? Is it possible to send information to an activity running in the background and somehow receive it in onResume()? Is there something else I can use?

Similar questions did not seem helpful so I apologize for repeating this. I don't see any reason to provide any code. Will do so if anyone asks.

EDIT: Solution of a follow up problem I received after using startactivityforresult()

I did not mention it above, but I am using a recycler view adapter class to get from activity A to activity B. This made it so that I was not able to change startActivity() to startactivityforresult().

My original piece of code was:

 mContext.startActivity(intent);

to solve the problem it was changed to the following:

((Activity) mContext).startActivityForResult(intent, 1);
Vasko Vasilev
  • 554
  • 1
  • 10
  • 25
  • 1
    Clearly seems a case of [startactivityforresult](https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android) . – ADM Jul 12 '19 at 08:52
  • Perhaps I did not word it as I intended. Let me edit my question. – Vasko Vasilev Jul 12 '19 at 09:01
  • You did alright . And `startactivityforresult` is way to go . read the answeres at above link . – ADM Jul 12 '19 at 09:03
  • Check this question, https://stackoverflow.com/questions/920306/sending-data-back-to-the-main-activity-in-android – Asad Mukhtar Jul 12 '19 at 10:39

1 Answers1

0

Activity A to B,

Intent i = new Intent(A.this, B.class);
i.putExtra("book_id", 123);
startActivityForResult(i, 1);


// in Activity A

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            String result=data.getStringExtra("auther_name");
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            //for no result
        }
    }
}//onActivityResult

Activity B to A,

Intent returnIntent = new Intent();
returnIntent.putExtra("auther_name", "name");
setResult(Activity.RESULT_OK,returnIntent);
finish();

Hope this works.

Hardik Chauhan
  • 2,750
  • 15
  • 30