onRestoreInstanceState is not being called

Put your problem here if it does not fit any of the other categories.

onRestoreInstanceState is not being called

Postby andreleitao » Tue May 05, 2009 8:05 pm

Hi folks,

I would like to know why my Activity does not calls onRestoreInstanceState() method when it is resumed from an interruption (by pressing Home key, for example).

I save my app state as following:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         @Override
  3.  
  4.         protected void onSaveInstanceState(Bundle outState) {
  5.  
  6.                 outState.putInt("OPA", 1);
  7.  
  8.                 super.onSaveInstanceState(outState);
  9.  
  10.        }
  11.  
  12.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


And would like to retrieve my data as following:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3.         protected void onRestoreInstanceState(Bundle savedInstanceState) {
  4.  
  5.                 super.onRestoreInstanceState(savedInstanceState);
  6.  
  7.  
  8.  
  9.                 int a = savedInstanceState.getInt("OPA");
  10.  
  11.                 Log.d("MainActivity", "Is back: " + a);
  12.  
  13.        
  14.  
  15.         }
  16.  
  17.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Buuut, onRestoreInstanceState is not called :(

Why that?

Thanks
andreleitao
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Mar 10, 2009 9:56 pm
Location: Recife - Brazil

Top

Postby Emmanuel7 » Wed May 06, 2009 12:00 am

I didn't even notice this method :)

For exactly this purpose, I just use the Bundle that is given in the OnCreate method.

From the activity documentation page :
"<...>
In addition, the method onSaveInstanceState(Bundle) is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, to be later received in onCreate(Bundle) if the activity needs to be re-created.
<...>"

That being said, from the documentation, I don't fully understand why OnRestoreInstanceState is not called.

Hope it helps,
Emmanuel7
Senior Developer
Senior Developer
 
Posts: 164
Joined: Fri Dec 12, 2008 2:17 am
Location: Paris

Postby ErnestoGuevara » Thu May 07, 2009 6:24 am

I think there are some situations that don't qualify as a restore, like if a user hits the back button to go back to the previous activity on the stack and then returns to the last activity by opening it again - in that case its not a restored state so much as a new one. At least I think it's something like that.
Search "Awesome Android Apps" in the market
http://www.awesomeandroidapps.com
ErnestoGuevara
Experienced Developer
Experienced Developer
 
Posts: 75
Joined: Thu Jan 08, 2009 2:02 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 17 guests