Question

I come from the web world, I'm new to Android.

In the web world, you usually pass the id of things, from the "list view" to the "detail view", example:

URL: Messages/List --> Message/Detail?messageId=x

In Android, I have this activity with all my messages: MessagesActivity
They are fetched from the db (SQLite) and displayed in a ListView with a custom made Adapter.

Question: Is it inefficient to only pass the id of the message (in the intent extras) from this activity to MessageDetailActivity ?

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {               
        Intent intent = new Intent(thisActivityContext, MessageDetailActivity.class);
        intent.putExtra("messageId", id); // inefficient? should pass object?
        startActivity(intent);
    }

As opposed to passing the entire message object. I'm asking this because I'm not finding very intuitive the usage of a "Tag" in my view, that I would have to cast...

Also, holding all the "Message" objects in tags would use a lot of memory maybe

Was it helpful?

Solution

If your object is relatively light, you could make its class implement Serializable and pass it as en extra to the MessageDetailActivity, if you think the object is heavy (many fields and nested objects inside) you should pass the Id and fetch it again from the MessageDetailActivity.

Keep in mind though that fetching a record from Sqlite on Android is fast on most devices, so its ok to fetch a couple of records on your new activity (as long as you do it on a background thread to keep the UI from locking)

However one thing I've found is that if you need to modify that object on db, you will save yourself a lot of trouble if you just fetch the whole objet from DB everytime any of the activities is created, since that way you will avoid sync issues.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top