0

I developed an application on Android 2.3.3. When I click a button in application, I open next Activity via Intent and populate Linear Layout with data in onCreate method. Then I go to the next activity. When I click back button, previous Activity is loaded with all the data. My application works fine on Android 2.3.5, but when I tested it on Android 4.0, the previous activity is missing data.

I also implemented back button action. I call this.finish() on Activity.

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
Loadeed
  • 557
  • 2
  • 8
  • 18

1 Answers1

1

I would recommend looking into saving the instance state of your application. This can be done by using a combination of onSaveInstanceState and onRestoreInstanceState. This way all data is retained by the Activity.

@Override        
protected void onSaveInstanceState(Bundle savedInstanceState) {
   super.onSaveInstanceState(savedInstanceState);            
   savedInstanceState.putInt("data", a);
}

@Override    
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);     
    a= savedInstanceState.getInt ("data");   
} 
jimmithy
  • 6,360
  • 2
  • 31
  • 29
  • My data is ArrayList of some classes, which I read from SQLiteDatabase. I am thinking about implementing onStart() method and reading data from database each time activity is started. That way, user will always have fresh data available, if it is going back to that activity or is creating activity for the first time. Is this the proper approach? – Loadeed Nov 14 '12 at 08:03
  • I would recommend using onResume() instead if you want fresh data each time the user visits the activity. onStart is only called when the Activity is created/restarted, onResume is called every time the Activity is shown on screen. Take a look at the Activity Lifecycle for more information: http://developer.android.com/reference/android/app/Activity.html – jimmithy Nov 14 '12 at 14:22