2017-01-07 10 views
0

戻るボタンを1回クリックすると前の画面に移動し、2回クリックするとアプリケーションが終了します。ワンクリックで以前のアクティビティに戻る方法と、アプリケーションを終了する方法を2回クリックする方法

私はたくさん試してstackoverflowの質問を参照してくださいしかし私はここに質問を入れて私の問題を解決することはできません。

NAvigation.java

@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } 
    if (doubleBackToExitPressedOnce) { 
     super.onBackPressed(); 
     return; 
    } 
    else { 

     backButtonHandler(); 
     // Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show(); 
    } 

} 


public void backButtonHandler() { 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(
      Navigation.this); 
    // Setting Dialog Title 
    alertDialog.setTitle("Leave application?"); 
    // Setting Dialog Message 
    alertDialog.setMessage("Are you sure you want to leave the application?"); 
    // Setting Icon to Dialog 
    alertDialog.setIcon(R.drawable.m_visit); 
    // Setting Positive "Yes" Button 
    alertDialog.setPositiveButton("YES", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        finish(); 
       } 
      }); 
    // Setting Negative "NO" Button 
    alertDialog.setNegativeButton("NO", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        // Write your code here to invoke NO event 
        dialog.cancel(); 
       } 
      }); 
    // Showing Alert Message 
    alertDialog.show(); 
} 

私はこの方法を試してみましたが、これは唯一の出口アプリケーションのための私を助けています。

答えて

0

private int clickCount = 0; 
private long delay = 100; 
Timer timer = new Timer(); 
@Override 
public void onBackPressed() { 
    if (clickCount == 2) { 
    timer.cancel(); 
    //operations to be performed on double click 
    } else { 
    clickCount++; 
    timer.schedule(new TimerTask() { 
     @Override public void run() { 
     getActivity().runOnUiThread(new Runnable() { 
      @Override public void run() { 
      clickCount = 0; 
      //operations to be performed on single click 
      } 
     }); 
     } 
    }, delay); 
    } 
} 

このロジックを試してみてください、あなたがdelayフィールド

+0

そのが動作していないにクリック間たい遅らせるどんな設定..私は、単一のTYM戻るボタンを押したときにdailogが表示されますが、私はあることを示したいです2回クリックするとダイルする –

+0

私が追加したコードはちょうどサンプルです。ユーザが戻るボタンをダブルクリックすると 'if'ブロックが実行され、シングルクリックで' else'ブロックの 'run()'メソッドが実行されます。ワンクリックでダブルクリックするだけで、どんな操作でも追加できます。 – arjun

関連する問題