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.