App exit on double tab in android - android

I want to exit the app only when double tap continously. I am using fragment class. I used the below code but doesn't work
private long lastPressedTime;
private static final int PERIOD = 2000;
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
switch (event.getAction()) {
case KeyEvent.ACTION_DOWN:
if (event.getDownTime() - lastPressedTime < PERIOD) {
finish();
} else {
Toast.makeText(getApplicationContext(), "Press again to exit.",
Toast.LENGTH_SHORT).show();
lastPressedTime = event.getEventTime();
}
return true;
}
}
return false;
}
please guide me how to implement this in an app.

#Override
public void onBackPressed() {}
in your activity, because activity responsible for back button handling
Look to this question, it has not got an accepted answer, but contain some solutions

Remove your implementation inside onKeydown and try this code
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}

try this :
private boolean doubleBackToExitPressedOnce;
#Override
public void onBackPressed() {
Log.i(tag, "Start: On Back Pressed!");
if (doubleBackToExitPressedOnce) {
Log.i(tag, "Double Back Pressed!");
super.onBackPressed();
return;
}
doubleBackToExitPressedOnce = true;
Toast.makeText(this, R.string.msg_exit, Toast.LENGTH_SHORT).show();
Timer t = new Timer();
t.schedule(new TimerTask() {
#Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2500);
}
dont forget to add this line in onResume:
#Override
protected void onResume() {
doubleBackToExitPressedOnce = false;
}

Related

Need help regarding CountDownTimer in onbackpressed

I have been trying to create the onbackpressed() function using countdown but i don't know what to do. Kindly help me to create the onbackpressed function using countdown
private static boolean onbackpressonce = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// using countdown timer
#Override
public void onBackPressed() {
if (!onbackpressonce) {
Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();
onbackpressonce = true;
} else {
super.onBackPressed();
}
new CountDownTimer(3000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
onbackpressonce = false;
}
}.start();
}
This will exit your app on the second onBackpressed within 1100 millis
private long mLastBackClick = 0;
#Override
public void onBackPressed() {
if (System.currentTimeMillis() - mLastBackClick < 1100) {
super.onBackPressed();
} else {
Toast.makeText(MainActivity.this, "Press back again to Exit", Toast.LENGTH_SHORT).show();
mLastBackClick = System.currentTimeMillis();
}
}

Android back button app is not closing

i am using the below code for back button but it cant work it does not exit the app if i am pressing the back button of userdevice
private Boolean exit = false;
#Override
public void onBackPressed() {
if (exit) {
this.finish(); // finish activity
} else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}
private Boolean exit = false;
#Override
public void onBackPressed() {
if (exit) {
this.finishAffinity();
} else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}
Change Your Code Like This.
private Boolean exit = false;
#Override
public void onBackPressed() {
if (exit) {
this.finishAffinity();
} else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}
use this...
private long lastBackPressTime = 0;
#Override
public void onBackPressed() {
if (this.lastBackPressTime < System.currentTimeMillis() - 4000) {
//Add Snhackbar or Toast, whatever you want
this.lastBackPressTime = System.currentTimeMillis();
} else {
if (toast != null) {
toast.cancel();
}
moveTaskToBack(true);
}
}
keep a boolean toClose=false;
and in onBackPressed() method use this code
if (toClose)
super.onBackPressed();
else
{
toClose=true;
Utils.showToast(context,getString(R.string.back_again));
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
toClose=false;
}
},2000);
}
This may work. Use finishAffinity() instead.
boolean doubleBackToExitPressedOnce = false;
public void onBackPressed(){
if(doubleBackToExitPressedOnce)
{
finishAffinity();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click back again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce = false;
}
},3500);
}
Use this to close the app on pressing back button.
#Override
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
This will work for you
Define this it globally
private boolean doubleBackToExitProceed = false;
#Override
public void onBackPressed() {
if (doubleBackToExitProceed) {
finish();
return;
}
doubleBackToExitProceed = true;
Toast.makeText(this, "Press Back again to Exit.", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitProceed = false;
}
}, 2000);// time in milliseconds.
}

In Android where to put the global method?

Below is my code to exit my app.
Since I have more than 1 activity, where should I put exitBy2Click() so it can be use for all activities?
I tried to create a new class called "Global", and public exitBy2Click(), but Toast.makeText(this,... not work.
Thanks.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
exitBy2Click();
}
return false;
}
private static Boolean isExit = false;
private void exitBy2Click() {
Timer tExit = null;
if (!isExit) {
isExit = true;
Toast.makeText(this, "click again to quit", Toast.LENGTH_SHORT).show();
tExit = new Timer();
tExit.schedule(new TimerTask() {
#Override
public void run() {
isExit = false;
}
}, 2000);
} else {
finish();
System.exit(0);
}
}
Checking your method, I think that best option would be Inheritance..
Note on example below that you can override onBackPressed() instead of onKeyDown()
public class BaseActivity extends Activity {
private static Boolean isExit = false;
#Override
public void onBackPressed() {
exitBy2Click();
}
public void exitBy2Click() {
Timer tExit = null;
if (!isExit) {
isExit = true;
Toast.makeText(this, "click again to quit", Toast.LENGTH_SHORT).show();
tExit = new Timer();
tExit.schedule(new TimerTask() {
#Override
public void run() {
isExit = false;
}
}, 2000);
} else {
finish();
System.exit(0);
}
}
}
Then, your "real" activities can extends that BaseActivity and this way, onKeyDown and exitBy2Click will be commom to all classes.
public class MainActivity extends BaseActivity {
#override
public void onCreate(Bundle savedInstance) {
}
}
public class SecundaryActivity extends BaseActivity {
#override
public void onCreate(Bundle savedInstance) {
}
}
//ETC
The best place to put this code is literally "nowhere".
Forcibly terminating an Android app is not recommended, and calling System.exit is definitely not something you should ever do in an Android app.

Logout when the application is force closed

I want to implement the function : if user dosen't checked the autoLogin CheckBox, clear the login user information and logout when the application is quit normally or force closed(clean the memeory).
I write the code clearAutoStart() to clear user information both in finish() and OnDestory().
When the user press back button twice, the finish() will execute, and will logout success. But as you know, if the application force closed, OnDestory() will not execute all the time. So in this situation, it will not logout success.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
exitBy2Click(); //quit normal
}
return false;
}
private static Boolean isExit = false;
private void exitBy2Click() {
Timer tExit = null;
if (isExit == false) {
isExit = true;
Toast.makeText(this, "press again to quit", Toast.LENGTH_SHORT).show();
tExit = new Timer();
tExit.schedule(new TimerTask() {
#Override
public void run() {
isExit = false;
}
}, 2000);
} else {
finish();
System.exit(0);
}
}
#Override
protected void onDestroy() {
clearAutoStart();
super.onDestroy();
}
#Override
public void finish() {
clearAutoStart();
super.finish();
}
#Override
protected void onStop() {
Log.i("ws", "---->>SmarterActivity onStop");
super.onStop();
}
public void clearAutoStart() {
RememberUser rememberUser = RememberUser.getInstance();
if (rememberUser.getIsAutoStart() == false) {
Log.i("ws", "---->>clearAutoStart getIsAutoStart false ");
UserLocalStore userLocalStore = UserLocalStore.getInstance();
userLocalStore.setUserLoggedIn(false);
userLocalStore.clearUserData();
Log.i("ws", "---->>clearAutoStart getIsAutoStart false OK ");
} else {
Log.i("ws", "---->>clearAutoStart getIsAutoStart true ");
}
}
I try to add the clearAutoStart() in onStop(), but this means if I press the HOME button , the application will logout.
When you press HOME onPause() is called and then onStop().
So you can put a member variable that keep the track of what is happening:
#Override
protected void onPause() {
mIsPause = true;
super.onPause();
}
#Override
protected void onStop() {
Log.i("ws", "---->>SmarterActivity onStop");
if(!mIsPause)
clearAutoStart();
super.onStop();
}

My Application is not exiting when I double tap the Hardware Back Button

I'm trying to exit my application when the user double taps the HardWare Back Button, I've used the following code in my application:
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
Dashboard_Activity.this.finish();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit",
Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
Here when the user Double Taps the Hardware Back Button the same activity appears again and again, but the app doesn't exits. Can you please help me fixing the issue.
Try the following:
private static long back_pressed;
#Override
public void onBackPressed()
{
if (back_pressed + 2000 > System.currentTimeMillis()) super.onBackPressed();
else Toast.makeText(getBaseContext(), "Press once again to exit!", Toast.LENGTH_SHORT).show();
back_pressed = System.currentTimeMillis();
}
Source
Try This Method Any where in your code Out side of Oncreate, Working for me
#Override
public void onBackPressed()
{
try
{
//pop up Window closed.
if (doubleBackToExitPressedOnce)
{
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
First, create SplashScreenActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finish();
}
Then in your activity(or in BaseActivity) add this variable as class member:
private long milis;
And override onBackPressed():
#Override
public void onBackPressed() {
if(milis + 2000 > System.currentTimeMillis()){
Intent intent = new Intent(this, SplashScreenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}else{
milis = System.currentTimeMillis();
}
}

Categories

Resources