Reputation:
In my application I want to show the the Dialog-box
when press the back button
.But Logcat gives me error like :
E/WindowManager( 3044): Activity com.MAT.Canadian.Immi.Test.Play.Display_questions has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4053e1c8 that was originally added here
E/WindowManager( 3044): android.view.WindowLeaked: Activity com.MAT.Canadian.Immi.Test.Play.Display_questions has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4053e1c8 that was originally added here
Into my case I can see the Dialog-Box
for the some seconds after that it goes without press Ok/cancel
button. I don't know how this happen,Please let me know where I do mistake.
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Toast.makeText(getApplicationContext(), "close the quiz app", Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Display_questions.this.finish();
//dialog.dismiss();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
Check whole My code :
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz_questions);
listview = (ListView) findViewById(R.id.questions_list);
listview.setItemsCanFocus(false);
GoToNextQuestion();
}
private void GoToNextQuestion() {
// TODO Auto-generated method stub
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> myAdapter, View myView, int pos, long mylng) {
String selectedFromList = (String) listview.getItemAtPosition(pos);
SelectedAnswer.setAnswer(selectedFromList);
if (!checkAnswer()) return;
if (currentGame.isGameOver()){
Intent i = new Intent(Display_questions.this, Display_result.class);
i.putExtra("Timer_Value", TimerTime);
startActivity(i);
finish();
}
else{
GoToNextQuestion();
}
}
});
setQuestions();
}
private void setQuestions() {
String question = currentQ.getQuestion().trim();
TextView qText = (TextView) findViewById(R.id.txt_questions);
qText.setText(question);
// set the available options
List<String> answers = currentQ.getQuestionOptions();
adapter = new ListviewAdapter(this,answers);
listview.setAdapter(adapter);
}
private boolean checkAnswer() {
//getSelectedAnswer();
// logic for the check answers
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Toast.makeText(getApplicationContext(), "close the quiz app", Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Display_questions.this.finish();
//dialog.dismiss();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
Thanks a lot.
Upvotes: 1
Views: 1378
Reputation: 1234
Change onBackPressed() to this
@Override
public void onBackPressed() {
// super.onBackPressed();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
//dialog.dismiss();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
Upvotes: 3