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:
I've overridden the onSaveInstanceState and onRestoreInstanceState handlers. Here's an example from my login Activity:
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?
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?
It is creating a new Activity on the stack. How do I make this work?