1. Download our Official Android App: Forums for Android!

Apps savedInstanceState not working?

Discussion in 'Android Development' started by andywhoa, Jul 22, 2010.

  1. andywhoa

    andywhoa Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    38
    Posts:
    105
    Joined:
    May 15, 2010

    May 15, 2010
    105
    13
    38
    Software Engineer
    West Des Moines, IA
    I have two Activities in my app and I've created an option menu to go back and forth between them

    Example from one of the Activities:
    Code (Text):
    1.     @Override
    2.     public boolean onCreateOptionsMenu(Menu menu) {
    3.         super.onCreateOptionsMenu(menu);
    4.         MenuInflater inflater = getMenuInflater();
    5.         inflater.inflate(R.menu.menu, menu);  
    6.         return true;
    7.     }
    8.    
    9.     @Override
    10.     public boolean onOptionsItemSelected(MenuItem item) {
    11.         super.onOptionsItemSelected(item);
    12.         switch(item.getItemId()) {
    13.             case R.id.menu_inbox:
    14.                 Intent intentLaunchInbox = new Intent(ActivityLogin.this, ActivityInbox.class);
    15.                 startActivity(intentLaunchInbox);
    16.                 break;
    17.         }
    18.         return true;
    19.     }
    I've overridden the onSaveInstanceState and onRestoreInstanceState handlers. Here's an example from my login Activity:
    Code (Text):
    1.     @Override
    2.     protected void onSaveInstanceState(Bundle outState) {      
    3.         // save screen state
    4.         outState.putString(LOGIN_USER_NAME, m_txtUsername.getText().toString());
    5.         outState.putString(LOGIN_USER_PASS, m_txtPassword.getText().toString());
    6.         outState.putSerializable(LOGIN_STATE_MODEL, m_smLogin);
    7.         super.onSaveInstanceState(outState);
    8.     }
    9.    
    10.     @Override
    11.     public void onRestoreInstanceState(Bundle savedInstanceState)
    12.     {
    13.         super.onRestoreInstanceState(savedInstanceState);
    14.        
    15.         // restore screen state
    16.         if (savedInstanceState != null) {
    17.             if (savedInstanceState.containsKey(LOGIN_USER_NAME)) {
    18.                 m_txtUsername.setText((String)savedInstanceState.getString(LOGIN_USER_NAME));
    19.             }
    20.             if (savedInstanceState.containsKey(LOGIN_USER_PASS)) {
    21.                 m_txtPassword.setText((String)savedInstanceState.getString(LOGIN_USER_PASS));
    22.             }
    23.             if (savedInstanceState.containsKey(LOGIN_STATE_MODEL)) {
    24.                 m_smLogin = (StateModel)savedInstanceState.getSerializable(LOGIN_STATE_MODEL);
    25.                 // if thread is still running, throw up another progress dialog
    26.                 if (m_smLogin.getState() == StateModel.State.RUNNING) {
    27.                     m_pdProgress = ProgressDialog.show(ActivityLogin.this, "", getString(R.string.logging_in), true);
    28.                 }
    29.             }
    30.         }
    31.         updateDisplay();
    32.     }
    When I navigate away from the login Activity, it's onSaveInstanceState gets called and the State is written.

    If I click the BACK button on the emulator, it goes back to the login screen and everything is populated (onRestoreInstanceState is not called. it's like it just goes back to a saved screen on the stack). However, if I click the MENU button to open the options menu, a new login Activity is created and the savedInstanceState is null (and onRestoreInstanceState is never called).


    What's going on? How do I make this work right?
     

    Advertisement

  2. andywhoa

    andywhoa Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    38
    Posts:
    105
    Joined:
    May 15, 2010

    May 15, 2010
    105
    13
    38
    Software Engineer
    West Des Moines, IA
    Ah, so the restore only happens if the Activity was killed.

    The way I am going about going back and forth is really creating new Activities on the Activity stack. What I need to do is tell my Intent to pull the existing Activity off the history stack and move it to the front.

    The needs to be doing similar to
    Code (Text):
    1. Intent.setFlag(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
    before starting the Activity.

    Unfortunately, it's not working :( It is creating a new Activity on the stack. How do I make this work?
     
  3. andywhoa

    andywhoa Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    38
    Posts:
    105
    Joined:
    May 15, 2010

    May 15, 2010
    105
    13
    38
    Software Engineer
    West Des Moines, IA
    Code (Text):
    1. android:launchMode="singleInstance"
     
  4. andywhoa

    andywhoa Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    38
    Posts:
    105
    Joined:
    May 15, 2010

    May 15, 2010
    105
    13
    38
    Software Engineer
    West Des Moines, IA
    Hmm

    This presents a new issue...

    Is there a programmatic way to go through all the Activities on the Activity stack and destroy them?
     
  5. markb

    markb Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    148
    Joined:
    Sep 14, 2009

    Sep 14, 2009
    148
    26
    63
    Software Developer (Java, C, Web, Unix)
    Birmingham, UK
    Hi andywhoa

    I haven't tried it myself, but is this any use to you?

    There's a noHistory attribute

    <activity> | Android Developers

    Mark
     
  6. cp1

    cp1 Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    375
    Joined:
    Apr 30, 2010

    Apr 30, 2010
    375
    35
    53
    Does your ActivityLogin have to keep running, or is it done once it logs you in? If it finishes it should kill itself when the memory is needed.
     
  7. KennethWindish

    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Jun 19, 2010

    Jun 19, 2010
    1
    0
    5
    I believe you need to capture the key press when the back button is hit & have it save the instance.
     
  8. rkjhaw

    rkjhaw Lurker
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Jul 12, 2010

    Jul 12, 2010
    1
    0
    5
    Software Engineer
    Bangalore, India

Share This Page

Loading...