0

In android my activity is not stopping when I am returning to the main menu by back buttons.

So i used below code to stop the application from running. But when I hit back button it's showing Unfortunately,application has stopped. So is there a simple way where I stop my application from running when I return to main menu or by back button.

protected void onDestroy() {
        this.finish();
      }

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lightsui"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/My_Theme" 
       >
        <activity
            android:name="com.example.lightsui.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>
    </application>

</manifest>
eswaat
  • 733
  • 1
  • 13
  • 31
  • 1
    You should not call `finish()` in `onDestroy()` since your activity is already finishing. Are you override the back key? – Simon Jan 26 '14 at 19:56
  • @Simon No I am not overriding the back key. I have only one screen. So what I want is when I hit back key my app should not be running. But without using the above code it was running that's why i used the above code. – eswaat Jan 26 '14 at 19:59
  • Finish could actually call onDestroy, so you are creating a loop. When you return to main menu, you app just pauzes (onPauze). This will not call onDestroy immediately (only when Android terminates the app). – RobinDeCroon Jan 26 '14 at 20:00
  • @RobinDeCroon `onDestroy()` is not guaranteed to be called unless you call `finish()`. – Simon Jan 26 '14 at 20:01
  • @Simon Thanks, I based my comment on this post: http://stackoverflow.com/a/19892032/1741111 Thats why I was confused. – RobinDeCroon Jan 26 '14 at 20:08
  • @Simon is there a specific time when our application is killed or stop in android or we have to do it manually by using some method. – eswaat Jan 26 '14 at 20:08
  • You should not try to kill your app. Let Android manage that. Both of the answers below are good. – Simon Jan 26 '14 at 21:35
  • What do you mean by "app is still running"? – David Wasser Jan 27 '14 at 10:28

1 Answers1

0

It seems like you dont want the activity to be present when you go back, thats easy to achieve (it is stopping itself but just resuming when you return via the back key)

Add the following flag to the activity in your manifest to get the behaviour you require:

  android:noHistory="true"
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124