2016-05-17 159 views
0

我想在android設備的後退按鈕上顯示提示消息,詢問用戶是否確定他的天氣。如果用戶按「是」,那麼他應該導航到前一個。在「否」的情況下,應恢復活動。但是,當我按下按鈕它執行這兩個操作時會發生問題。它也導航到以前的活動,並顯示警報對話框..這是我的代碼..看看並指導我..在此先感謝。顯示按下後退按鈕上的提醒對話框

public void onBackPressed() { 

    super.onBackPressed(); 

    AlertDialog.Builder alertdialog=new AlertDialog.Builder(this); 

    alertdialog.setTitle("Warning"); 
    alertdialog.setMessage("Are you sure you Want to exit the tutorial???"); 
    alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 

      Intent intent=new Intent(secAddition.this,addition.class); 
      startActivity(intent); 

      secAddition.this.finish(); 

     } 
    }); 

    alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
     } 
    }); 


    AlertDialog alert=alertdialog.create(); 
    alertdialog.show(); 



}} 
+0

不要調用startActivity上BackPress。你應該調用超級OnBackPress。當按下硬件後退時。檢查我的答案。 –

回答

3

我實現了一個slimier類型的事情,請檢查下面的代碼片段

@Override 
    public void onBackPressed() { 
      //super.onBackPressed(); 
      IsFinish("Want to close app?"); 
     } 


    } 

    public void IsFinish(String alertmessage) { 
      DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        switch (which) { 
         case DialogInterface.BUTTON_POSITIVE: 
          android.os.Process.killProcess(android.os.Process.myPid()); 
          // This above line close correctly 
          //finish(); 
          break; 

         case DialogInterface.BUTTON_NEGATIVE: 

          break; 
        } 
       } 
      }; 

      AlertDialog.Builder builder = new AlertDialog.Builder(context); 
      builder.setMessage(alertmessage) 
        .setPositiveButton("Yes", dialogClickListener) 
        .setNegativeButton("No", dialogClickListener).show(); 

     } 
1

刪除

super.onBackPressed(); 

這應該工作..

一樣,

public void onBackPressed() { 

AlertDialog.Builder alertdialog=new AlertDialog.Builder(this); 

alertdialog.setTitle("Warning"); 
alertdialog.setMessage("Are you sure you Want to exit the tutorial???"); 
alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 

     Intent intent=new Intent(secAddition.this,addition.class); 
     startActivity(intent); 

     secAddition.this.finish(); 

    } 
}); 

alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     dialog.cancel(); 
    } 
}); 


AlertDialog alert=alertdialog.create(); 
alertdialog.show(); 



}} 
+0

它的工作..謝謝 –

+0

友好標記爲答案。它的工作原理是 –

5

如果你想顯示的警告對話框中不關閉應用程序或清除棧,您不應該致電 super.onBackPressed();

+0

。謝謝 –

1

,你可以這樣寫你的代碼工作:

public void onBackPressed() { 
AlertDialog.Builder alertdialog=new AlertDialog.Builder(this); 

alertdialog.setTitle("Warning"); 
alertdialog.setMessage("Are you sure you Want to exit the tutorial???"); 
alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     super.onBackPressed();  
    } 
}); 

alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     dialog.cancel(); 
    } 
}); 


AlertDialog alert=alertdialog.create(); 
alertdialog.show(); 

} 
} 

無需啓動Activity如果再你要以前Activity

在你的代碼,當你做一回機, super.onBackPressed()被調用如此這般以前Activity

2

這是你在找什麼。你不需要在按回來時啓動活動。 如果用戶選擇是不是叫超級onBackPress

public void onBackPressed() { 
AlertDialog.Builder alertdialog=new AlertDialog.Builder(this); 
alertdialog.setTitle("Warning"); 
alertdialog.setMessage("Are you sure you Want to exit the tutorial???"); 
alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 

    super.onBackPressed(); 

} 
}); 

alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
    dialog.cancel(); 
} 
}); 


AlertDialog alert=alertdialog.create(); 
alertdialog.show(); 
}}