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:
Can someone give me some advice on what is the best practice?
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.
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.
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() .
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.
As compare to DB Query use Intent.
And another way is, use one common class which will holds your data temporary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With