如何在片段[Android]上双击背面实现退出

时间:2015-10-13 14:27:18

标签: android android-fragments

我想在我的应用程序中的一个FRAGMENT(不是MainActivity)中实现双后退 我写了这段代码,但它没有用。

这是我的片段:

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;
        }
    });

任何建议..

3 个答案:

答案 0 :(得分:2)

此代码应放入MainActivity,我假设您的第一个页面片段名称为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;
}

答案 1 :(得分:1)

我认为你的问题是你在其他地方以毫秒为单位反对时间,然后当你再次点击时,你的背压增加了1毫秒,但你可能等了超过1毫秒再次按下,所以你的反击仍然不会高于新的当前时间毫秒,所以它总会进入其他。

您应该将当前时间加上2000或3000(2或3秒)添加到其他地方的背景中,然后将其与新时间进行比较。

在评论和你的回答之后,请试试这个。

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;
  }
});

答案 2 :(得分:0)

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;
  }
});