6

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?

Joshua Hyatt
  • 1,300
  • 2
  • 11
  • 22

9 Answers9

6

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>
elifekiz
  • 1,456
  • 13
  • 26
5

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.

Kapil Rajput
  • 11,429
  • 9
  • 50
  • 65
Nicolas Defranoux
  • 2,646
  • 1
  • 10
  • 13
5

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).

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
3

finishAffinity();

This is simply exit from the app

2

Calling finish() at any Activity will go through its life cycle to destroy it. For sure destroying the fragment will remove it from the back stack.

So, you have to call finish(); method from your splash activity first, then start the MainActivity using intent as following

finish();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent); 

Yoi can also solve this problem by overriding the method OnBackPressed with an empty one without calling Super.onBackPressed ();

Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
1

EDIT: just to be clear, this is not the right way of exiting from an application. Use at your own risk. The OP was asking for A way of exiting the application, even though it was not the correct way.

System.exit(0)

Is this what you're looking for?

Sebastiano
  • 12,289
  • 6
  • 47
  • 80
  • No, this only exits the current Activity. – Joshua Hyatt Mar 01 '14 at 20:41
  • 3
    @nKn I know it should not be used. To be fair, the whole question (as stated by the author) is a "I should not do this but still..". So I don't really think I deserve a -1. – Sebastiano Mar 01 '14 at 20:42
  • 1
    @John The Android documentation clearly states `Causes the VM to stop running and the program to exit with the given exit status.` for the exit() method. How is it that it exits just the current activity? (source: http://developer.android.com/reference/java/lang/System.html#exit(int)) – Sebastiano Mar 01 '14 at 20:43
  • Using `System.exit()` is a generally bad practice and should be avoided because it goes against the Android ideology of how a application closing is handled. You might read more on this here http://stackoverflow.com/questions/17899668/android-app-behaving-unexpectedly-after-calling-system-exit0 – nKn Mar 01 '14 at 20:46
  • 2
    @dextor Just tested it. When I call System.exit(0), it opens the splash screen. I don't know why, maybe lousy implementation on Google's part, but it doesn't work. – Joshua Hyatt Mar 01 '14 at 20:47
  • @nKn Again, I know it. And again, we both agree that the traditional way is to let the OS handle the exiting of an application. But since the OP was asking for **A** way of exiting, a way of exiting I provided. I guess the author of the OP knows that perfectly well. – Sebastiano Mar 01 '14 at 20:50
  • 1
    Essentially, you are both right here - everything belonging to the app (unless it explicitly is multi-process) *is* exited. But then the things that are still, in Android's opinion, supposed to be around, will get re-created in a new process. So to be effective, you have to convince Android that the things you want to exit from are no longer supposed to be around, and just calling System.exit() does not do that - rather, Android treats it somewhat like the case of having to dispose of a background process to free up memory, and then re-create it later when it comes to the foreground. – Chris Stratton Mar 04 '14 at 20:00
1

Even if you don't want it, I'm sorry to tell you that the only way of exiting cleanly is using finish(). If you're having some problems closing Activities in the background, there's another solution for it, check this link.

Basically there's no one-click or all-at-once way of accomplishing this, and don't use System.exit(), it's a bad practice, as you may read here.

Community
  • 1
  • 1
nKn
  • 13,691
  • 9
  • 45
  • 62
1

Finish is the way to go. You can actually close your SplashScreen activity once it is no longer needed. Ie. after SplashScreen is no longer needed, just call finish on it, after starting your main activity.

Also, its not that finish() will close your application, even after calling it process for your app will still exists, all your static variables will be still valid, of course until android decides to kill your app which can happen long after your finish call.

marcinj
  • 48,511
  • 9
  • 79
  • 100
0

I totally agree with you that finish() is somehow not practical but anyways you can use it simply and all will be "kosher" with best practices I suppose.

Here is a short tutorial for you to handle "Exit" from any other activity in your app:

I assume that splash screen is the first activity in your app.

  1. If you also have a main/home screen in your app then finish the splash activity right after the main/home has been called with Intent from your Splash activity with e.g.

startActivity(new Intent(getApplication(), Home.class)); Splash.this.finish();}

After doing so, follow the step 2. but instead of overriding the onResume in splash screen override it in the Home/Main screen.

2. If your user needs access to the splash screen later, do not finish() your splash screen but override its onResume method as follows:

@Override protected void onResume() { super.onResume(); try { Intent i = getIntent(); Boolean isExit = i.getBooleanExtra("isExitAction",false); if(isExit){ this.finish(); } } catch (Exception e){} }

3. In the activity from which you want to close the application, use the following code to exit where it suits you the most. Do not forget to substitute for Home.class depending which is your first unfinished activity.

        Intent i = new Intent(this, Home.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.putExtra("isExitAction", true);
        startActivity(i);

WHAT IT DOES: If you press a button from your 5th activity which fires the code in the 3rd step it finishes all previous activities 4,3,2 and "redirects" you to activity 1, which is your Home (or Splash). After the redirection, the onResume() is called, and it finishes the last remaining activity. The application reaches this.finish() only if isExitAction is true