Tianxiang Xiong
Tianxiang Xiong

Reputation: 4091

Android: When and how to save application data

I'm writing a little recipe management app. On the main screen there is a button called "Add recipe". When clicked, a new activity is created that consists of a TabHost with several tabs.

The first tab is "Basic Information" and contains a TextView called "Name", into which the user may put the name of the recipe. A second tab is called "Ingredients" and contains a ListView. Through the Options Menu corresponding to this tab, there is an option called "Add Ingredient", which launches a new activity with some TextView objects into which ingredient information may be entered.

My question is, when should a new recipe entry be added to an SQLite database table? From the "Saving Persistent State" section of the documentation it seems like I should create a new entry right after launching the "Add Recipe" activity. However, at that point I have not yet entered a recipe name. Should I:

  1. Create a temporary name for the recipe entry right upon activity launch and update it later?
  2. Wait until name and ingredients have been entered, then get user to hit a "confirm" button?

Or some other method?

I'm storing the ingredients for all recipes into an SQLite table and identifying which recipe an ingredient belongs to through a foreign key. Therefore, to store the ingredients to the "Ingredients" table, I need to have a recipe already in the "Recipes" table.

Upvotes: 0

Views: 556

Answers (2)

colig
colig

Reputation: 395

I would get the app to store temporary incomplete recipe information when the activity is obscured in some way -- eg. onPause() and onSaveInstanceState(). When the user calls up the activity again, just restore the data. Only when the user actually presses a save button would the app enter the data into SQLite.

Have a look at Parcelable for an idea of how to store this data temporarily.

Upvotes: 1

Nissan911
Nissan911

Reputation: 293

create your own content provider. http://developer.android.com/guide/topics/providers/content-providers.html

Upvotes: 0

Related Questions