Suppose you are showing list of movies.
You are offering a filter (as a separate activity) (Filter window could be overlayed on top of the list of movies (not sure how I could do this in android)
Now user selects filters in the filter screen, and wants to apply the filter.
The filter data should be transfered from the filter activity to the previous activity (which shows the movie lists).
How can I pass the data in this scenario?
Should I use some kind of event system (such as NotificationCenter in ios)?
Or should I use some form of startActivity even though the activity is already started and waiting.
When you want to open a particular Activity to get result back in previous Activity, Best practice according to me is use startActivityForResult(intent,requestCode) instead of startActivity(intent).
Override following method is your 1st activity where you wish to receive data
onActivityResult(int requestCode, int resultCode, Intent data)
In order to get result back on 1st Activity, following code should be triggered in 2nd Activity
Intent resultIntent = new Intent();
resultIntent.putExtra("data", yourData); //make 'yourData' Serializable if a POJO
setResult(Activity.RESULT_OK, resultIntent);
finish();
After this code triggers you will get callback in onActivityResult() in your 1st activity where you called startActivityForResult() with whatever extars you put. Hope this helps. GL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With