Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Search filter in Android

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.

like image 622
eugene Avatar asked May 25 '26 13:05

eugene


1 Answers

  1. 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).

  2. Override following method is your 1st activity where you wish to receive data onActivityResult(int requestCode, int resultCode, Intent data)

  3. 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();
    
  4. 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

like image 87
NotABot Avatar answered May 28 '26 02:05

NotABot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!