Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Exit Android Application Without finish()

Tags:

java

android

Before you tell me that this question has already been asked, let me tell you that none of those questions have an answer that suits me. I have a main Activity for my app. Before it runs, I have a splash screen Activity. If I call finish(), the program returns to my splash screen. I do not want this. Do not tell me to use finish(). I have heard that it is bad practice to not let Android close the app on its own. I know what I am doing...probably....

I want to be able to completely close the application from the second activity. Does anyone know of a way I can do this?

like image 620
Joshua Hyatt Avatar asked Mar 01 '14 20:03

Joshua Hyatt


People also ask

How do you exit an app code on Android?

You can call System. exit(); to get out of all the acivities.

How do I force a programmatically to close another program?

writeBytes("kill "+ APP_PID + "\n"); os. flush();

How do I stop apps from running in my Android library?

If you want app to be stopped immediately then you have to press the Red stop button. Play button will stop the app only after gradle build is finished.


3 Answers

when you want to close your application, you can call

   finishAffinity();

or if you want close it in background also you should write,

android:excludeFromRecents="true"

in AndroidManifest :

   <activity
    android:name="com.smart.remote.main.SplashActivity"
    android:configChanges="orientation|keyboardHidden"
    android:screenOrientation="portrait" 
    android:excludeFromRecents="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
like image 196
elifekiz Avatar answered Sep 16 '22 18:09

elifekiz


Use finish() in your splash screen just before you create your second activity, then use finish() is the second activity: that will not bring back the splash screen.

like image 33
Nicolas Defranoux Avatar answered Sep 17 '22 18:09

Nicolas Defranoux


Executing finish() on the splash activity is one solution, but two alternative methods are:

  1. Start the splash activity with android:noHistory="true" in its manifest entry.
  2. Launch the secondary activity with the Intent flag FLAG_ACTIVITY_CLEAR_TOP.

My preference would be the first, as users are unlikely to want to ever see the splash screen in the history stack (or at all, for that matter, but that's a different discussion).

like image 37
Paul Lammertsma Avatar answered Sep 16 '22 18:09

Paul Lammertsma