1

As I'm creating an android application in which first I've created main activity, then I've added splash activity along with the one normal activity.

So my problem is whenever I click on exit in my app it closes the main activity and returns that one normal activity in splash activity. I am using finish(); on exit button which is present in main activity.

So how can I exit the app which comes to android launcher screen

I've also tried creating new intent with Action Main but its only minimizing the app I want to close it

MainActivity.class

if (id == R.id.exit) {
            AlertDialog.Builder builder = new AlertDialog.Builder(homeActivity.this);
            builder.setMessage("Do You Want To Exit?").setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.setIcon(R.drawable.ic_error_outline_black_24dp);
            alert.setTitle("Exit!!");
            alert.show();
      }
Nilesh Indalkar
  • 310
  • 4
  • 16
  • 1
    Possible duplicate of [How to exit from the application and show the home screen?](http://stackoverflow.com/questions/3226495/how-to-exit-from-the-application-and-show-the-home-screen) – Jakub Holovsky Jan 06 '17 at 08:16
  • dismiss your splash activity when you start main activity from it. – Pushpendra Jan 06 '17 at 08:17
  • it is happening because first activity is still in the stack – Prabs Jan 06 '17 at 08:19
  • This issue is not with Your MainActivity. While pushing your MainActivity, need to finish the SplashScreen and use tags as specified by @Trucket – Sreehari Jan 06 '17 at 09:40

4 Answers4

6

When starting the MainAcitivity from SplashActivity set flags in intent as below and call finish() on SplashAcitivty

Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
arjun
  • 3,514
  • 4
  • 27
  • 48
3

you can set android:excludeFromRecents="true" and android:noHistory="true" in you android Manifest file for the splash activity.

W4R10CK
  • 5,502
  • 2
  • 19
  • 30
Hector
  • 4,016
  • 21
  • 112
  • 211
0

Try adding flag "Intent.FLAG_ACTIVITY_CLEAR_TOP" while you launch MainActivity from your SplashScreenActivity, as below:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
KayPee
  • 173
  • 1
  • 1
  • 10
  • i have tried but its not working in following way i have added `Intent inst = new Intent(ipInfo.this,homeActivity.class); inst.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(inst);` – Nilesh Indalkar Jan 06 '17 at 09:44
  • after starting the activity i have set `finish();` then it works as i expected thanks :) – Nilesh Indalkar Jan 06 '17 at 09:47
0

add this things in splash while navigating splash to mainActivity

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
finish();
faiyaz meghreji
  • 271
  • 1
  • 2
  • 9