Reputation: 381
I want to implement double back pressed in one of my FRAGMENT (NOT MainActivity) my app I have written this code but its not working.
this is my fragment :
public class DashBoard extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dashboard, container, false);
rootView.setFocusableInTouchMode(true);
rootView.requestFocus();
rootView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (back_pressed++ > System.currentTimeMillis()) {
Log.d("Time backpressed..if", "" + back_pressed + 2000);
Log.d("Time system..if", "" + System.currentTimeMillis());
getActivity().finish();
} else {
Toast.makeText(getActivity(), "Press once again to exit!", Toast.LENGTH_SHORT).show();
back_pressed = System.currentTimeMillis();
Log.d("Time backpressed..else", "" + back_pressed);
Log.d("Time system..else", "" + System.currentTimeMillis());
}
}
return true;
}
return false;
}
});
any suggestions..
Upvotes: 0
Views: 1423
Reputation: 338
This code should put in MainActivity
and I assume your first page fragment name is HomeFragment
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (isInHomeFragment() && !doubleBackToExitPressedOnce) {
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "press double tap to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
} else if (isInHomeFragment() ) {
finishAffinity();
} else {
super.onBackPressed();
}
}
private boolean isInHomeFragment() {
for (Fragment item : getSupportFragmentManager().getFragments()) {
if (item.isVisible() && "HomeFragment".equals(item.getClass().getSimpleName())) {
return true;
}
}
return false;
}
Upvotes: 2
Reputation: 23
I think your problem is that you're giving backpressed the time in millis in the else, and then when you click again you add 1 millisecond to your backpressed, but you probably waited more than 1 millisecond in pressing back again, so your backpressed will still not be higher than the new current time millis, so it will always go into the else.
You should add the current time plus 2000 or 3000 ( 2 or 3 seconds) to your backpressed in else, and then compare it with the new time.
After the comments and your answer, please try this.
public class DashBoard extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dashboard, container, false);
rootView.setFocusableInTouchMode(true);
rootView.requestFocus();
long back_pressed=System.currentTimeMillis();// Backpressed initialized //
rootView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (back_pressed++ > System.currentTimeMillis()) {
Log.d("Time backpressed..if", "" + back_pressed);
Log.d("Time system..if", "" + System.currentTimeMillis());
getActivity().finish();
else {
Toast.makeText(getActivity(), "Press once again to exit!", Toast.LENGTH_SHORT).show();
back_pressed = (System.currentTimeMillis()+2000);
Log.d("Time backpressed..else", "" + back_pressed);
Log.d("Time system..else", "" + System.currentTimeMillis());
}
return true;
}
return false;
}
});
Upvotes: 1
Reputation: 381
public class DashBoard extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dashboard, container, false);
rootView.setFocusableInTouchMode(true);
rootView.requestFocus();
long back_pressed=System.currentTimeMillis();`//****this my intialization of back_pressed***//
rootView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {`if (back_pressed++ > System.currentTimeMillis()) {
Log.d(
Log.d("Time system..if", "" + System.currentTimeMillis());
getActivity().finish();
} else {
Toast.makeText(getActivity(), "Press once again to exit!", Toast.LENGTH_SHORT).show();
back_pressed = (System.currentTimeMillis()+2000);
Log.d("Time backpressed..else", "" + back_pressed);
Log.d("Time system..else", "" + System.currentTimeMillis());
}
return true;
}
return false;
}
});
Upvotes: 0