I have a dialog that pops up in my activity. I want it to finish the entire activity when the back button is pressed on the dialog. How would I do this?
set the onCancelListener for the dialog and call finish() when its called.
setOnCancelListener(new OnCancelListener()
{
#Override
public void onCancel(DialogInterface dialog) {
removeDialog(DIALOG_ALERT);
finish();
}
})
Related
i have two activities. On 1st activity i have button after click on button it will show dialog window and after click on dialog button i will jump to 2nd activity.
On 2nd acitivity i have custom button which is calling onBackPressed() method:
private void btnBackListener(){
btnBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
My Problem is when im on 2nd activity and i call onBackPressed(); it will jump into 1nd activity but also dialog is visibile. How to prevent showing that dialog after btnBack clicked in 2nd activity?
In your case, I think best option would be to close dialog before opening 2nd activity.
I am not sure this workaround is the correct way to achieve my goal of having a prompt text in a spinner. What happens with this application is the spinner navigates to another Activity via an Intent and when the user navigates back to the Main Activity with the spinner they have two ways back. One with a Button and a click event the other by clicking the device BACK button. I am trying to call the code in the click event from the method that manages the device BACK button
I do not know how to call the click event from the device BACK button Method
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Use BACK BUTTON\n\n"+"On the Screen",Toast.LENGTH_LONG).show();
// I want to call goBack(View view) from here
// +++++++++++++++++++++++++++++++++++++++++++
}
public void goBack(View view){
Intent i = new Intent( PageTwo.this, MainActivity.class );
startActivity( i );
}
The reason I use this Intent to navigate BACK to the Main Activity is it reloads the variables in the Spinner
It looks like goBack(View) is most likely from an onClick setup in your layout XML. Since you aren't using the view, just pass null:
#Override public void onBackPressed() {
goBack(null);
}
I don't know if I get you right, if you just want to go back to the activity which started another activity, you can just call finish() method of Activity class:
#Override
public void onBackPressed() {
finish();
}
finish() reference
I am trying to implement simple back button on activity to go to previous activity, but some weird behavior is happening when I am calling method finish() - the problem is I have to press back button TWO times? Why is this happening? In the back method i have only finish(). What is the other way to go to previous saved activity without instantiate a completely new Intent?
public void back1 (View view){
this.finish();
}
This is second try with the same result:
ImageButton buttonback = (ImageButton) findViewById(R.id.imageButton6);
buttonback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
You are most likely starting the activity twice.
When pressing back you are finishing one, and the other one is coming forward. Check your onCreate and onResume for anything that may be using intents or starting any activity
#Override
public void onBackPressed() {
super.onBackPressed();
}
add that to your code the super call closes the activity no need to call finish()
If you just want the devices back button to function, you dont need to override the onBackPressed method in Activity.
If you want to place a custom button in view and set back action to that button, then you need to write button clicklistener to the same
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
I have a custom dialog which extends the Dialog Class, I would like to bind an event to execute some code after the Dialog is closed when the user presses the BACK button of the device. How can I do that? I found a post where someone says that the .addWindowListener() should be used when working with Dialogs and other Window widgets. But the dialog class doesn't have the addWindowListener method, so I cannot use it. Is there another way without using fragments cause I shouldn't re-write the MyCustomDialog class?
This is the code:
public class MyCustomDialog extends Dialog {
public MyCustomDialog(Context context, int layoutResourceId) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(layoutResourceId);
}
}
Thanks for the attention!
EDIT: i found this on the android developers site, is there a way to use it with MyCustomDialog class?
onDismiss DialogInterface
Since you are extending android Dialog class you can implement a Dismiss Listener in your Activity's and set it when you create the Dialog, and then in the listener implement any functionality you want depending on the button that was used to dismiss the Dialog.
Hope this will solve your problem.
Edit You can use dialog.setCanceledOnTouchOutside(false); which will stop closing the dialog if you touch outside of the dialog.
Something like,
Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(false);
OR Alternatively
Override onTouchEvent() of dialog and check for action type. if the action type is
'MotionEvent.ACTION_OUTSIDE' means, user is interacting outside the dialog region. So in this case, you can dimiss your dialog or decide what you wanted to perform.
view plainprint?
dialog.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_OUTSIDE){
Toast.make(getApplicationContext(), "TOuched outside the dialog", Toast.LENGTH_LONG).show();
this.dismiss();
}
return false;
}
});
And for back press you can do dialog.setCancelable(false); which will prevent dialog getting cancelled from backpress event.
OR you can alternatively override setOnKeyListener event and put your own code into it.
Edit
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
#Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
dialog.dismiss();
}
return true;
}
});
Happy Coding!!!
You need to Override onBackPressed inside Dialog class. Also make sure to close dialog after override OnBackPressed .
Try this
public class MyCustomDialog extends Dialog {
public MyCustomDialog(Context context, int layoutResourceId) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(layoutResourceId);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
dismiss(); // make sure to call dismiss to close dialog
// put your code here
}
}
If you want to trigger an event if user clicked/touched outside the dialog and close it or used the back button to close then use
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
//your trigger goes here
Toast.makeText(IntroductoryActivity.this, "on cancel", Toast.LENGTH_SHORT).show();
}
});
But if you want to trigger an event if something dismisses the dialog, like some other event then use
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
//your trigger goes here
Toast.makeText(IntroductoryActivity.this, "on dismiss", Toast.LENGTH_SHORT).show();
}
});
I am running a service that starts a new activity when specific
applications are launched.
For example, when I launch sms application, my service detects it by
checking a top activity package name and starts a new activity.
But the problem is that after starting a new activity, when I finish
that activity and press BACK button from sms application to go back to
Home screen, it does not finish my sms application.
Even though the screen is at home(launcher), when I check top activity
name, sms app is running as the top activity, which means sms app is
not finished after pressing BACK button.
I use Intent.FLAG_ACTIVITY_NEW_TASK intent flag for starting a new
activity and finish() to finish it. Does anyone have an idea why my
BACK button does not finish sms application in this case?
thanks,
777
From what I've seen, the back button will halt the current activity, whatever it's doing. If you absolutely need to finish it off, take a look at the lifecycle of an activity, and perhaps put some code into the onPause() and onStop() functions.
OK if it doesnt workout.. try overriding the OnBackPressed method
and put finish() in that.. hope this helps
If you want to finish the activity when user navigate to back through device So you can use this code it is more helpful.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
I did something like that in my activity:
#Override
public void onBackPressed(){
AlertDialog.Builder alert = new AlertDialog.Builder(ctx);
alert.setTitle("Wylogowanie i wylaczenie.");
alert
.setMessage("Exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//DO SOMETHING YOU NEED eg. destroy/close DB, stop services, and move the task to back
db.getWritableDatabase(); //getDB
db.destroyDB(context); //Destroy it
db.close(); //and close
stopService(new Intent(getBaseContext(), SVCKeepLogged.class)); //stop some service
moveTaskToBack(true); //hide activity (this do not kill it - you can use finish() here)
//finish();
}
})
.setNegativeButton("NOOO!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
//Keep app alive.
dialog.cancel();
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
try this
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
// TODO
}
#Override
public boolean onOptionItemSelected(MenuItem item){
if(item.getItemId() == android.R.id.home){
finish();
}
}