0

I have two exit scenarios in my application :-

1) whenever user enters wrong password for 3 consecutive times, my app should exit and gps notifications must start. For this i have written code as follows :-

 if(value.length() >= authenticationCode.length() && !value.equals(authenticationCode))
    {
            dataEnteredEditText.setError("Invalid Code!!!");

            dataEnteredEditText.setText(AntiTheftConstant.EMPTY_STRING);

            countAttempts++;

            if(countAttempts == 3)
            {
                SharedPreferences.Editor editor = sharedPref.edit();

                editor.putBoolean(AntiTheftConstant.USER_AUTHENTICATION_STATUS, false);

                editor.commit();

                registerForLocationUpdates();

                dataEnteredEditText.removeTextChangedListener(this);

                startProcessForAuthenticationFailure();

                moveTaskToBack(true);
            }
    }

moveTaskToBack() minimizes my activity and moves it to background. But i don't want this sort of behaviour. I want my application to completely exit with no acitivities in minimized state or in background.

2) User is asked for one time password(OTP) and if he enters correct password GPS notification should stop and my application should again exit. For this scenarion i have written following code:-

   if((authenticationCode.equals(sharedPref.getString(AntiTheftConstant.GENERATED_PASSWORD, AntiTheftConstant.DEFAULT_APPLICATION_AUTHENTICATION_CODE))))
    {
            editor.putBoolean(AntiTheftConstant.USER_AUTHENTICATION_STATUS, true);

            editor.commit();

            stopService(new Intent(applicationContext, EmailMonitoringService.class));

            stopService(new Intent(applicationContext, LocationDetector.class));

            AlarmManager am=(AlarmManager)applicationContext.getSystemService(Context.ALARM_SERVICE);

            Intent i = new Intent(applicationContext, TheftAlarmReceiver.class);

            PendingIntent pi = PendingIntent.getBroadcast(applicationContext, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

            am.cancel(pi);

        //  Intent intent = new Intent(Intent.ACTION_MAIN);
        //                 
        //  intent.addCategory(Intent.CATEGORY_HOME);
        //                 
        //  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        //  startActivity(intent);

            this.finish();
    }

I have tried both code blocks below:-

this.finish()

And

Intent intent = new Intent(Intent.ACTION_MAIN);

intent.addCategory(Intent.CATEGORY_HOME);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

But none of them is working fine. Both code blocks above are moving my current activity to background but none of them makes my application exit gracefully.

If you see any problem with above approaches please do suggest. Thanks in advance.

user3279837
  • 65
  • 1
  • 6

3 Answers3

2

finish() will push the application to the background. It doesn't "exit" the application per-se, it rather closes the current activity. If it's the only open activity, it "closes" the application.

Android decides when to actually release the resources associated with the activity and application and good practice says: "let Android decide when to drop the resources". For more info, read this.

If you really have to clear the application completely (can be dangerous if you don't close certain resources and events first) it can technically be done using android.os.Process.killProcess(android.os.Process.myPid());

Community
  • 1
  • 1
JRad the Bad
  • 511
  • 5
  • 25
  • how can i ensure that only one activity is present at one time. When moving from one activity to other i thought that previous one automatically gets closed. Is it so? As it is mentioned above that finish() closes current activity and in case only one activity is present it closes application. how can i ensure that only one activity should be running at one point of time. How can i close previous activity when moving from one activity to other? Thanks in Advance. – user3279837 Mar 12 '14 at 12:58
  • I see what you're trying to accomplish here. You're probably having issues with the back button sending you to screens you thought were closed. I had this before. You just need to tell the app to destroy those activities upon leaving them. Open your manifest and under every activity you want "destroyed" after you leave, add `android:noHistory="true"` . That should do it. Let me know if you need anything else. – JRad the Bad Mar 12 '14 at 17:33
  • if your problem has been solved, you may try accepting an answer. It's customary. – JRad the Bad Mar 13 '14 at 20:35
  • Voting up an answer needs 15 reputation points, I only have 6. – user3279837 Mar 24 '14 at 15:56
  • You can always accept an answer as the original Poster though. Just click the Check Mark and it will mark it as the accepted answer. You should also get some reputation points for it. – JRad the Bad Mar 24 '14 at 17:41
  • If i use 'android.os.Process.killProcess(android.os.Process.myPid());' to close my app, would my background services and registered broadcast receivers also get cleared? I want to retain them in background. – Usman Rana Dec 21 '16 at 12:45
0

try this You must call this.finish() after super.onCreate(...)

ali shekari
  • 740
  • 1
  • 11
  • 26
0

If you want to finish something in an Activity you should just use:

finish();

If you want to finish something in a Fragment you should use:

getActivity().finish();