我有一个基于Wordpress的新闻应用程序。为了加快速度,我购买了一个满足大部分需求的模板。但是在应用程序中,当我按下主页或任何第一级屏幕上的按钮时,它会退出应用程序。
如果用户按两次后退按钮,我该如何实现退出?
答案 0 :(得分:0)
试试这个,
private static final int TIME_INTERVAL = 2000; // # milliseconds, desired time passed between two back presses.
private long mBackPressed;
@Override
public void onBackPressed()
{
if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis())
{
super.onBackPressed();
return;
}
else { Toast.makeText(getBaseContext(), "Tap back button in order to exit", Toast.LENGTH_SHORT).show(); }
mBackPressed = System.currentTimeMillis();
}
答案 1 :(得分:0)
在多次执行相同的行为后,决定为它提出一个android库。 DoubleBackPress Android Library
,处理所有麻烦,并提供简单的模板使用。
Example GIF of similar behavioural requirements
首先,将依赖性添加到您的应用程序中:
dependencies {
implementation 'com.github.kaushikthedeveloper:double-back-press:0.0.1'
}
现在,实施:
// set the Action to occur on DoubleBackPress
DoubleBackPressAction doubleBackPressAction = new DoubleBackPressAction() {
@Override
public void actionCall() {
// TODO : your code to exit the application
}
};
// setup DoubleBackPress behaviour
DoubleBackPress doubleBackPress = new DoubleBackPress()
.withDoublePressDuration(3000) //msec - timeout
.withDoubleBackPressAction(doubleBackPressAction);
@Override
public void onBackPressed() {
doubleBackPress.onBackPressed();
}