Press Back twice to Exit


import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private static boolean userPressedBackAgain;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

@Override
public void onBackPressed() {

if(!userPressedBackAgain){
Toast.makeText(this, "Back again to exit", Toast.LENGTH_LONG).show();
userPressedBackAgain=true;
} else {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}

new CountDownTimer(3000,1000){
@Override
public void onTick(long millisUntilFinished) {

}

@Override
public void onFinish() {
userPressedBackAgain =false;
}
}.start();
}
}

Deixe um comentário