0

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?

sn87_uk
  • 1
  • 1
  • 1
    https://stackoverflow.com/questions/14462456/returning-from-an-activity-using-navigateupfromsametask – Pavneet_Singh Sep 21 '18 at 20:54
  • Unfortunately the link doesn't help - I did a bit of further investigation and I've found out from the logs that the NewsArticles activity is actually destroyed when starting an intent to the Settings activity which is why it navigates back to main activity. Why would it do this? I had thought it would be added to the back stack. – sn87_uk Sep 22 '18 at 16:22

0 Answers0