3

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.

eugene
  • 39,839
  • 68
  • 255
  • 489
  • 1
    In android its a bit simple. When you are using a seperate Activity for filter, start the activity with startActivityForResult. Once You are done selecting filter values, finish that activity and the onResult method will be called in previous activity which had your list. You can filter the list there. Refer - https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android – Kapil G Jul 25 '17 at 13:38

2 Answers2

1
  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

NotABot
  • 781
  • 7
  • 24
0

Implement the Runnable interface and add UI changes you wish to perform inside the run method. Then instantiate and send it to the main UIThread with runOnUiThread. Your changes will then take place in the main activity (where you have a list of movies).

Example of how to use runOnUiThread.

RonTLV
  • 2,376
  • 2
  • 24
  • 38