0

I am working in android. I am designing a file upload functionality.

This is my layout:

enter image description here

If I fill my entries like as title,price,category and then I press browse button for browse option then I go to BrowseActivity using startActivity(). But when I come back to this activity then all entries which were filled by me disappear. please suggest me what should I do for this so my entered entries save. If I do browse first then fill entries then it works properly.

What should I do for the case in which user first fill entries and then click on the browse button?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Pushpendra Kuntal
  • 6,118
  • 20
  • 69
  • 119
  • Reto Meier explains it really well in his response to ["How to save an Android application's state?"](http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state/151940#151940) – Raunak Oct 13 '11 at 15:35

3 Answers3

3

Use the SharedPreferences options to save all the Strings in onPause(). You can get them back in onResume() and fill out your text boxes. This is also nice since you can save the values that were entered and restore them each time the user leaves the app for any other reason.

EDIT:

Quick example:

Save your strings like so:

@Override
public void onPause(){
   super.onPause();
   // Save the user preferences during runtime for later use.
   SharedPreferences settings = getSharedPreferences("aName", MODE_PRIVATE);
   SharedPreferences.Editor editor = settings.edit();
   editor.putString("title", "Value of title");
   editor.commit();
}

Then you get get the value back like this.

@Override
public void onResume(){
   SharedPreferences settings = getSharedPreferences("aName", MODE_PRIVATE);
   String title = settings.getString("title");
}
DeeV
  • 35,865
  • 9
  • 108
  • 95
2

Here is what you need to do.

 @Override
 public void onSaveInstanceState(Bundle savedInstanceState) {
 // Save UI state changes to the savedInstanceState.
 // This bundle will be passed to onCreate if the process is
 // killed and restarted.
 savedInstanceState.putString("title", title.getText.toString());
 //Do the same for the other 2 boxes.

  super.onSaveInstanceState(savedInstanceState);
  }

and in onRestoreInstanceState() pull the items out.

Like..

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
 // Restore UI state from the savedInstanceState.
 // This bundle has also been passed to onCreate.
String Title = savedInstanceState.getString("title");
//Do the same for other String items you put into the bundle using the savedInstanceState() above. And then use EditText.setText(title); to set the text


}

The bundle is used exactly for this purpose. You Can get more info here

coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118
1

Check out android's shared preferences, they should give you an easy way to store data.

Android Shared Preferences

http://developer.android.com/guide/topics/data/data-storage.html

http://developer.android.com/reference/android/content/SharedPreferences.html

Another way is to create an object which holds the data you need, and don't new it in the onResume method...

Community
  • 1
  • 1
eplewis89
  • 112
  • 11