I'm having an issue where I have a Settings Activity which can be launched from either the MainActivity or a NewsArticles activity (MainActivity is parent of NewsArticles) and I want to navigate back to the logical parent. Looking at the documentation it seems that you have to declare the parent activity in the manifest:
<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=".NewsArticles"
android:label="@string/news_articles_title"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivity"/>
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/settings_title"
android:parentActivityName=".NewsArticles">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".NewsArticles" />
</activity>
So I have declared my Settings Activity to have NewsArticles activity as its parent. But the Settings Activity could also be launched from the MainActivity - how can I handle this situation? In my SettingsActivity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
ButterKnife.bind(this);
setSupportActionBar(mToolbar);
ActionBar actionBar = this.getSupportActionBar();
if(actionBar!= null)
actionBar.setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
//FIXME when user press back it always goes back to main activity regardless
//The settings activity can be launched from either the main activity or the news articles activity
//So we want to navigate up from the same task.
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
I am aware that as user navigates through activities it should be creating a task of activities and if user presses back or the up button it should navigate up from logical parent that started it. My app is in launchmode singletop and in this case I always end up back to MainActivity even though NewsArticles activity was declared parent - can anyone advise?