8

I am trying to understand what happens under the hood when the user presses on back and I did not overwrite onOptionsItemSelected.

I have the following scenario:

  1. Activity A with intent.putExtra("abc", "abc");
  2. Activity B with getIntent() in onCreate()
  3. Activity C

What exactly happens when the user clicks back in activity C?

If I don't overwrite the back button, pressing back will produce a nullException on the getIntent line. Why? It's in onCreate() not in onStart().

If I use onSaveInstanceState the bundle is always empty. This leads me to assume that back is creating a new instance of the Activity. Why? It should just finish itself.

If I overwrite the back button with finish() it no longer crashes but I thought the default behavior of back is to run finish().

EDIT with more details:

I put a toast in each of the lifecycle methods and I found out that Activity B calls on destroy after I press back on Activity C. This makes no sense to me!

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>

Activity C

<activity
    android:name=".activities.OptionActivity"
    android:configChanges="orientation"
    android:screenOrientation="portrait"
    android:label="@string/option_title"
    android:parentActivityName=".activities.MenuActivity">
</activity>
Mika
  • 5,807
  • 6
  • 38
  • 83
  • WHat happens when user clicks back in activity C depends on if there was a "finish()" called from Activity B before calling Activity C – Pararth Jul 03 '14 at 10:48
  • There is no `finish()` in activity B which is why I don't get it. – Mika Jul 03 '14 at 12:31
  • 1
    Can we see your code pls? And the log as well. – DrkStr Jul 03 '14 at 13:02
  • I have no code in Activity C that's the whole point I can write code to prevent the crash but I want to understand why I have to. I do not overwrite the back button at all. The line that causes the crash is in Activity B: `this.getIntent().getExtras().getString(STATE_REST)` and the activity shows a nullException because it's being recreated and there is no intent. My question is why does Activity B get destroyed when I click back on Activity C. – Mika Jul 03 '14 at 13:09
  • Can you post your manifest file? It may be helpful to see how you specified 'Activity B' there. – Ridcully Jul 03 '14 at 13:30

1 Answers1

1

Why does Activity B get destroyed when I click back on Activity C ?

Are you using intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); for intent before starting your activity. It will clear the backstack and when you press back, the activity will be recreated.

UPDATE:

Don't rely on the call for onDestroy() as it may or may not be called on back button press. However it is guaranteed to be called on a call to finish() but is not surely gets called on a back button press.

Activity OnDestroy never called?

Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
  • No I do not but that would indeed call `onDestroy()`. – Mika Jul 03 '14 at 13:47
  • You mean that the `onDestroy()` method is not called for Activity C but called for Activity B ? – sjain Jul 03 '14 at 14:03
  • It's also called (correctly) for C but it should not be called for B. – Mika Jul 03 '14 at 14:14
  • Don't rely on the call for onDestroy() as it may or may not be called on back button press. Check this - http://stackoverflow.com/questions/4449955/activity-ondestroy-never-called – sjain Jul 03 '14 at 14:23
  • I'm not relying on it. I am saying it should not be called when I hit back but for some reason it is. Always! This should not happen per the documentation as it should only be called when the system needs the space. – Mika Jul 03 '14 at 14:30
  • No guarantee. Why it should not be called when you hit back button. The system may still call it. Did you checked other devices or systems with enough space ? – sjain Jul 03 '14 at 14:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56718/discussion-between-mikael-and-ved-prakash). – Mika Jul 03 '14 at 14:33