I'm having an issue with activity navigation and I can't figure out what I'm doing wrong.
I have a MainActivity
and a SettingsActivity
but using the Back
and Up
(on the action bar) have two different results from the Settings Activity.
For example If I press the Back button I get the following lifecycle callbacks within the MainActivity
:
V/lifeCycle: onOptionsItemSelected
V/lifeCycle: onPause
V/lifeCycle: onSaveInstanceState-Bundle[..]
V/lifeCycle: onStop
<< PRESS BACK BUTTON >>
V/lifeCycle: onRestart
V/lifeCycle: onStart
V/lifeCycle: onResume
V/lifeCycle: onPostResume
While if I press the Navigate UP button i get these results:
V/lifeCycle: onOptionsItemSelected
V/lifeCycle: onPause
V/lifeCycle: onSaveInstanceState-Bundle[..]
V/lifeCycle: onStop
<< PRESS NAV UP >>
V/lifeCycle: onDestroy // Problem
V/lifeCycle: onCreate // Seems
V/lifeCycle: onStart // Here
V/lifeCycle: onResume
V/lifeCycle: onPostResume
The issue is when I press the Nav UP
my Main Activity gets destroyed and recreated meaning I loose all my view states, but pressing the back button does not do this.
I'm not sure if it's how I'm starting the PreferenceActivity
:
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
Or how my intents in AndroidManifest.xml
are configured:
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity">
<intent-filter>
<category android:name="android.intent.category.PREFERENCE" />
</intent-filter>
</activity>
That might be causing the issue, or if this is just the correct behaviour that I need to override and if so, what is the "correct" way to override?
Remove
android:parentActivityName=".MainActivity"
from your manifest file for SettingsActivity.
Edit: Above step no longer necessary.
In your SettingsActivity class, do the following:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case android.R.id.home:
this.finish();
return (true);
}
return super.onOptionsItemSelected(item);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With