1

Why does the back tubbon call onDestroy() on the parent?

I have the following scenario:

  1. Activity A that opens Activity B via an intent

    Intent intent = new Intent(parent.getContext(), activityB.class);
    intent.putExtra(STATE_REST, gson.toJson(myObject));
    startActivity(intent);
    
  2. When I click on back on activity B (and only then) activity A fires onDestroy() followed by onCreate().

Manifest:

Activity A

<activity
    android:name=".activities.MainActivity"
    android:configChanges="orientation"
    android:screenOrientation="portrait"
    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 B

<activity
    android:name=".activities.MenuActivity"
    android:configChanges="orientation"
    android:screenOrientation="portrait"
    android:label="@string/menu_title"
    android:parentActivityName=".activities.MainActivity">
</activity>
Mika
  • 5,807
  • 6
  • 38
  • 83
  • 2
    add manifest declaration please. – Sergey Shustikov Jul 03 '14 at 13:35
  • Manifest is ok. Now add `onBackPressed` implementation and `onCreate` methods of both `Activities`. – Sergey Shustikov Jul 03 '14 at 13:42
  • And the question. Did you call `setResult`? If - YES - where you do that? – Sergey Shustikov Jul 03 '14 at 13:44
  • I want to use the default behavior so I don't overwrite onBackPressed and I don't call setResult and `onCreate()` should not play a role here because after the back button is pressed it should fire `onStart()` (in activity A) not not onCreate() (neither should it call `onDestroy()`) am I missing something? – Mika Jul 03 '14 at 13:46
  • Actually this is related on situation. Look on activity lifecycle. If You press back button backStack acitivity should be going to onResume state. BUT. If Other application need the memory - activity get behavior like from start application. – Sergey Shustikov Jul 03 '14 at 13:51
  • Yes I know that but this not where the issue is coming from. onDEstroy() gets call systematically after back is pressed the problem is not a random memory allocation issue. – Mika Jul 03 '14 at 13:53

1 Answers1

3

your problem seems to be resolved in the flowing link: Why is onDestroy always called when returning to parent activity?

Good luck.

Community
  • 1
  • 1
Med Besbes
  • 2,001
  • 6
  • 25
  • 38