如何在按下一次后退按钮时返回上一屏幕以及如何在按下两次后退按钮时退出应用程序?
How to go back to the previous screen in single time backbutton pressed and how to exit from application while back button pressed twice?
我想执行一个操作,比如当我按一次后退按钮时,它会移动到选定的屏幕,当我按两次后退按钮时,它会显示对话框并要求退出。
我在堆栈溢出中尝试了很多例子,但 none 对我有帮助..
navaigation.java
private int clickCount = 0;
private long delay = 100;
Timer timer = new Timer();
@Override
public void onBackPressed() {
if (clickCount == 2) {
super.onBackPressed();
timer.cancel();
} else{
clickCount++;
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
backButtonHandler();
}
});
}
}, delay);
}
}
public void backButtonHandler() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
Navigation.this);
// Setting Dialog Title
alertDialog.setTitle("Leave application?");
// Setting Dialog Message
alertDialog.setMessage("Are you sure you want to leave the application?");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.m_visit);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
我尝试过这种方式,但它无法正常工作...
嘿,如果用户第一次单击后退按钮 time.And 第二次,请检查我显示的 Snackbar 应用程序将退出。
private boolean mIsBackAlreadyPressed;
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if (mIsBackAlreadyPressed) {
mIsBackAlreadyPressed = false;
super.onBackPressed();
overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
} else {
mIsBackAlreadyPressed = true;
Snackbar.make(drawer, R.string.press_back_twice, Snackbar.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mIsBackAlreadyPressed = false;
}
}, 2000);
}
}
希望这个help.Happy编码。
试试这个代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int TIME_DELAY = 2000;
private static long back_pressed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed() {
if (back_pressed + TIME_DELAY > System.currentTimeMillis()) {
super.onBackPressed();
}
else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Are you sure, You wanted to exit");
alertDialogBuilder.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
});
alertDialogBuilder.setNegativeButton("No",new
DialogInterface.OnClickListener() {
Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
back_pressed = System.currentTimeMillis();
}
你能试试这个吗
private boolean exit=false;//declare in public
public void onBackPressed() {
// TODO Auto-generated method stub
if (exit) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
} else {
Toast.makeText(this, "Tap again to exit.", Toast.LENGTH_SHORT)
.show();
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}
试试这个:
private Boolean close_app = false;
@Override
public void onBackPressed() {
if (close_app) {
//finish(); // pressed twice
backButtonHandler();
} else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
close_app = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
close_app = false;
}
}, 3 * 1000);
}
}
这里的Handler
是处理后退的,只显示一个Toast
,如果3秒内再有后退,则显示退出应用程序的对话框
如果你按回去就可以去任何你想去的地方
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(Exit.this,Home.class);
startActivity(intent);
finish();
}
如果你现在双击后退按钮你可以退出试试下面的代码
private Boolean exit = false;
@Override
public void onBackPressed() {
if (exit) {
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);
}
}
我想执行一个操作,比如当我按一次后退按钮时,它会移动到选定的屏幕,当我按两次后退按钮时,它会显示对话框并要求退出。
我在堆栈溢出中尝试了很多例子,但 none 对我有帮助..
navaigation.java
private int clickCount = 0;
private long delay = 100;
Timer timer = new Timer();
@Override
public void onBackPressed() {
if (clickCount == 2) {
super.onBackPressed();
timer.cancel();
} else{
clickCount++;
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
backButtonHandler();
}
});
}
}, delay);
}
}
public void backButtonHandler() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
Navigation.this);
// Setting Dialog Title
alertDialog.setTitle("Leave application?");
// Setting Dialog Message
alertDialog.setMessage("Are you sure you want to leave the application?");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.m_visit);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
我尝试过这种方式,但它无法正常工作...
嘿,如果用户第一次单击后退按钮 time.And 第二次,请检查我显示的 Snackbar 应用程序将退出。
private boolean mIsBackAlreadyPressed;
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if (mIsBackAlreadyPressed) {
mIsBackAlreadyPressed = false;
super.onBackPressed();
overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
} else {
mIsBackAlreadyPressed = true;
Snackbar.make(drawer, R.string.press_back_twice, Snackbar.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mIsBackAlreadyPressed = false;
}
}, 2000);
}
}
希望这个help.Happy编码。
试试这个代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int TIME_DELAY = 2000;
private static long back_pressed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed() {
if (back_pressed + TIME_DELAY > System.currentTimeMillis()) {
super.onBackPressed();
}
else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Are you sure, You wanted to exit");
alertDialogBuilder.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
});
alertDialogBuilder.setNegativeButton("No",new
DialogInterface.OnClickListener() {
Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
back_pressed = System.currentTimeMillis();
}
你能试试这个吗
private boolean exit=false;//declare in public
public void onBackPressed() {
// TODO Auto-generated method stub
if (exit) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
} else {
Toast.makeText(this, "Tap again to exit.", Toast.LENGTH_SHORT)
.show();
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}
试试这个:
private Boolean close_app = false;
@Override
public void onBackPressed() {
if (close_app) {
//finish(); // pressed twice
backButtonHandler();
} else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
close_app = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
close_app = false;
}
}, 3 * 1000);
}
}
这里的Handler
是处理后退的,只显示一个Toast
,如果3秒内再有后退,则显示退出应用程序的对话框
如果你按回去就可以去任何你想去的地方
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(Exit.this,Home.class);
startActivity(intent);
finish();
}
如果你现在双击后退按钮你可以退出试试下面的代码
private Boolean exit = false;
@Override
public void onBackPressed() {
if (exit) {
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);
}
}