2015-10-13 69 views
0

我想實現雙重背壓在我的FRAGMENT(不MainActivity)之一我的應用程序 我寫了這段代碼,但它不工作。如何實現退出雙重背部按下片段[Android]

這是我的片段:

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

任何建議..

+0

只需創建一個接口,您的主要活動,並有關鍵事件觸發的方法在你的主要活動來關閉應用程序。 – nukeforum

+0

back_pressed back_pressed ++> System.currentTimeMillis()? 執行內部代碼嗎? –

+1

不是一個答案,而是後代:作爲一般規則,這不是一個很好的行爲來補充。如果可能的話,應用程序應該能夠退出並重新加載足夠快的速度,要求用戶使用額外的步驟進行確認不應該值得。如果恢復用戶離開的地方是沒有意義的,但也有例外,但這種情況很少見。 –

回答

1

我覺得你的問題是,你給backpressed在其他米利斯的時間,然後再次單擊你爲你的壓抑加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; 
    } 
}); 
+0

你給我的建議是什麼,我做了但沒有爲我工作 –

+0

你能用新的改變更新你的代碼嗎?你在哪裏聲明並初始化back_pressed變量? –

+0

ok ..檢查我的更新代碼作爲答案...。 –

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

此代碼應放在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; 
} 
相關問題