我有这个片段适配器:
public class TabsPagerAdapter extends FragmentPagerAdapter {
private Context context;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Main fragment activity
return new main();
case 1:
// Sensors fragment activity
return new sensors();
case 2:
// Display fragment activity
return new display();
case 3:
// Settings fragment activity
return new settings();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 4;
}
}
我正在尝试以这种方式通过双击和吐司来退出后退按钮:
private static final long DOUBLE_PRESS_INTERVAL = 2000000000;
private long lastPressTime;
public void onBackPressed() {
Toast.makeText(context,
Resources.getSystem().getString(R.string.kilepes_dupla),
Toast.LENGTH_SHORT).show();
long pressTime = System.nanoTime();
if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
// this is a double click event
System.exit(0);
}
lastPressTime = pressTime;
}
我不能做的是在吐司中获取上下文,因此我无法展示它。如何修改代码以使其正常工作?谢谢您的回答!
编辑1: 好的,本教程我正在使用: androidhive ,我想在每个选项卡上制作这个退出方法