Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing cursor between Activities

I think the following scenario is common, but I can't figure the way to implement it:

We have three Activities, and one database. We ask the user for an input to search in the database. The query should get none, one or may results.

  • If you get none you simple inform the user.
  • If you get only one, you show it in a new activity with the right view.
  • If you get many you show them in a list to let the user to chose the right one and then you pass that to the activity which with show the data in the case of one result only.

The problem is that, in order to know the number of results, you need to do the query (and get the cursor) in the first activity. And in the case you get more than one result you need to send the data (pass the cursor?)to the list acitivty. Doing the query again in the list activity can't be right, right?

I'm aware that you can share cursors by using a content provider, but as the activities are from the same application and the data is private (useless outside), don't see the point of making it avaliable to anyone else.

I read here around that you can crate a parcelable cursor and send it in bundle, but I'm not sure if that is the right use.

Any idea on how to address this?

Thanks in advance.

like image 834
Asincrono Avatar asked Sep 14 '26 12:09

Asincrono


1 Answers

Option #1: The query performed by the search activity is just SELECT _ID FROM... In the "none" case, it displays the message. In the "one" case, it passes the matching _ID to the detail activity to view the match by querying to get all needed columns for that one row. In the "many" case, it passes the search terms to the list activity, which runs the full query (including all columns needed to display the list).

Option #2: Merge the search activity and the list activity into one activity. Basically, consider "search and choose an item to view" as being a "UI transaction" and do that all within the one activity. The search activity would do a query sufficient to populate the list in the "many" case, displaying the list in an AlertDialog or in the main activity itself via a ViewFlipper or something. In the "none" case, it displays the message. In the "one" case -- or when the user taps an entry in the list -- it passes stuff to the detail activity to view the item.

Option #3: Move your search query to the list activity -- your search activity passes the search info to the list activity via extras, which does the rawQuery() in onCreate(). The list activity handles the "none" and "many" cases. In the "one" case, it just calls startActivity() on the detail activity and finish() so control returns to the search activity when the user presses BACK.

All else being equal, I'd probably go with option #2.

like image 174
CommonsWare Avatar answered Sep 16 '26 07:09

CommonsWare



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!