Android Search Filter Implementation
Let's say you are showing a list of movies.
You are suggesting the filter (as a separate activity) (The filter window can be overlaid on top of the movie list (not sure how I can do this in android)
Now the user selects filters on the filter screen and wants to apply the filter.
The filter data should be carried over from the filter activity to the previous activity (which shows movie lists).
How do I pass data in this scenario?
Should I use any event system (like NotificationCenter in ios)? Or should I use some form startActivity
even if the activity is already running and waiting.
source to share
-
If you want to open a specific
Activity
one to get the result in the previous oneActivity
, the best practice for me is to usestartActivityForResult(intent,requestCode)
insteadstartActivity(intent)
. -
Override the following method is your first activity where you want to receive data
onActivityResult(int requestCode, int resultCode, Intent data)
-
To return the result to the 1st operation, the following code must be run in the 2nd operation
Intent resultIntent = new Intent(); resultIntent.putExtra("data", yourData); //make 'yourData' Serializable if a POJO setResult(Activity.RESULT_OK, resultIntent); finish();
-
After running this code, you will get a callback in
onActivityResult()
in your first activity where you calledstartActivityForResult()
with whateverextars
you put. Hope this helps. GL
source to share
Implement the interface Runnable
and add the UI changes you want to make inside the method run
. Then create an instance and send it to the main UIThread using runOnUiThread . Then your changes will take place in the main activity (where you have a list of movies).
An example of how to use runOnUiThread .
source to share