Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to pass data via intent or query the database when needed?

I am just wondering what would be the better way to handle data in several activities in android.

Say I had two activities, A and B, that hold some views. First I load some data from a SQL database and inflate the views in A. Now, I want to start activity B, which uses the same set of data as A did.

Is it better to pass the data via Intent (putExtra()) and then inflate the views or is it better to query the database again and then inflate.

I am not sure about that, because both approaches seem to have their disadvantages:

  • Querying the database takes more time /more resources
  • Putting extra data to the intent makes it more complex, because of putting and getting the data (especially when working with more activities)

Can someone give me some advice on what is the best practice?

like image 922
user2426316 Avatar asked Aug 26 '13 08:08

user2426316


People also ask

What is the benefit of query over data records?

Using a query makes it easier to view, add, delete, or change data in your Access database. Some other reasons for using queries: Find specific quickly data by filtering on specific criteria (conditions) Calculate or summarize data.

Why do we need to query a database?

Primarily, queries are used to find specific data by filtering explicit criteria. Queries also help automate data management tasks, summarize data and engage in calculations. Other examples of queries include append, crosstab, delete, make a table, parameter, totals and updates.

What method adds or passes data to the intent?

You can add extra data with various putExtra() methods, each accepting two parameters: the key name and the value. You can also create a Bundle object with all the extra data, then insert the Bundle in the Intent with putExtras() .

How do you share data between activities?

This example demonstrates how do I pass data between activities in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

As compare to DB Query use Intent.

And another way is, use one common class which will holds your data temporary.

like image 104
Pratik Dasa Avatar answered Oct 26 '22 21:10

Pratik Dasa