• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps savedInstanceState not working?

  • Thread starter Thread starter Deleted User
  • Start date Start date
D

Deleted User

Guest
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:
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);  
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	super.onOptionsItemSelected(item);
    	switch(item.getItemId()) {
    		case R.id.menu_inbox:
    			Intent intentLaunchInbox = new Intent(ActivityLogin.this, ActivityInbox.class);
        		startActivity(intentLaunchInbox);
    			break;
    	}
    	return true;
    }

I've overridden the onSaveInstanceState and onRestoreInstanceState handlers. Here's an example from my login Activity:
Code:
    @Override
    protected void onSaveInstanceState(Bundle outState) {    	
    	// save screen state
    	outState.putString(LOGIN_USER_NAME, m_txtUsername.getText().toString());
    	outState.putString(LOGIN_USER_PASS, m_txtPassword.getText().toString());
    	outState.putSerializable(LOGIN_STATE_MODEL, m_smLogin);
    	super.onSaveInstanceState(outState);
    }
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        
        // restore screen state
        if (savedInstanceState != null) {
        	if (savedInstanceState.containsKey(LOGIN_USER_NAME)) {
        		m_txtUsername.setText((String)savedInstanceState.getString(LOGIN_USER_NAME));
        	}
        	if (savedInstanceState.containsKey(LOGIN_USER_PASS)) {
        		m_txtPassword.setText((String)savedInstanceState.getString(LOGIN_USER_PASS));
        	}
        	if (savedInstanceState.containsKey(LOGIN_STATE_MODEL)) {
        		m_smLogin = (StateModel)savedInstanceState.getSerializable(LOGIN_STATE_MODEL);
        		// if thread is still running, throw up another progress dialog
        		if (m_smLogin.getState() == StateModel.State.RUNNING) {
            		m_pdProgress = ProgressDialog.show(ActivityLogin.this, "", getString(R.string.logging_in), true);
        		}
        	}
        }
        updateDisplay();
    }

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?
 
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:
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?
 
Hmm

This presents a new issue...

Is there a programmatic way to go through all the Activities on the Activity stack and destroy them?
 
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.
 
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:
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);  
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	super.onOptionsItemSelected(item);
    	switch(item.getItemId()) {
    		case R.id.menu_inbox:
    			Intent intentLaunchInbox = new Intent(ActivityLogin.this, ActivityInbox.class);
        		startActivity(intentLaunchInbox);
    			break;
    	}
    	return true;
    }

I've overridden the onSaveInstanceState and onRestoreInstanceState handlers. Here's an example from my login Activity:
Code:
    @Override
    protected void onSaveInstanceState(Bundle outState) {    	
    	// save screen state
    	outState.putString(LOGIN_USER_NAME, m_txtUsername.getText().toString());
    	outState.putString(LOGIN_USER_PASS, m_txtPassword.getText().toString());
    	outState.putSerializable(LOGIN_STATE_MODEL, m_smLogin);
    	super.onSaveInstanceState(outState);
    }
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        
        // restore screen state
        if (savedInstanceState != null) {
        	if (savedInstanceState.containsKey(LOGIN_USER_NAME)) {
        		m_txtUsername.setText((String)savedInstanceState.getString(LOGIN_USER_NAME));
        	}
        	if (savedInstanceState.containsKey(LOGIN_USER_PASS)) {
        		m_txtPassword.setText((String)savedInstanceState.getString(LOGIN_USER_PASS));
        	}
        	if (savedInstanceState.containsKey(LOGIN_STATE_MODEL)) {
        		m_smLogin = (StateModel)savedInstanceState.getSerializable(LOGIN_STATE_MODEL);
        		// if thread is still running, throw up another progress dialog
        		if (m_smLogin.getState() == StateModel.State.RUNNING) {
            		m_pdProgress = ProgressDialog.show(ActivityLogin.this, "", getString(R.string.logging_in), true);
        		}
        	}
        }
        updateDisplay();
    }

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?
I believe you need to capture the key press when the back button is hit & have it save the instance.
 
Back
Top Bottom