How do we know how exactly user cancelled the dialog? - android

I use a cancellable dialog fragment with setCanceledOnTouchOutside(true). Now I want to distinguish two cases:
user cancels the dialog using Back button;
user cancels the dialog by touching outside of it.
Both actions lead to invocation of both onCancel() and onDismiss() listeners. Also these two listeners accept DialogInterface as a parameter, to there no any "event state" to check how exactly user has cancelled the dialog.
So what can I do?

To know if the user has pressed the back button of the device, override the method onBackPressed:
#Override
public void onBackPressed()
{
//here you could set a boolean to know if the user pressed the back button, and react accordingly when the dialog is closed.
backPressed=true;
}
For example.

Related

Terminating the ongoing process and current activity

I am stumped on this issue, my onBackPressed() method doesn't work when it has to. My scenario is as soon as activity starts, progress dialog shows up because I called asynctask.execute() in onCreate. When the process takes long time I want to give user a feature that he can dismiss the ongoing process(downloading data from the server) so I tried to dismiss the dialog and finish the activity when back button is pressed, but it's not working.
When I normally press back button after I have got the data, then the control seems to be flowing under onBackPressed().
Below is my code snippet:
public void onBackPressed() {
if(progressDialog != null && progressDialog.isShowing())
{
progressDialog.dismiss();
}
finish();
}
Is there any other way to give user an opportunity to cancel that anytime. Please suggest me how to make the entire process and activity terminated when the user presses back button.
For canceling the progress dialogue please set progress dialog cancel as true.
dialog.setCancelable(true);

Dismissing dialog on activity finish

In my app I have several activities one after the other. After my login screen I have Home screen and after that several screens. Now When user select device home button or power off button I want to display login screen when user again comes to my app and then Home screen. Rest all activity I am finishing it from my base class. Now till here I have done, My problem is when I show a dialog in some other activity and at that instance if user click on home or power button, then i am getting WINDOW LEAKED EXCEPTION.
Like I have TempActivity is displaying a dialog and user clicked home button so StoreActivity and TempActivity will finish but Dialog never got chance to be dismissed. So What would be the best way to deal with this situation.
Is there some better way to dismiss the dialog so that I don't get any exception.
Override onDestroy, there, check whether the dialog is present, if so, dismiss it.
dismiss() in onDestroy() doesn't solve this problem. Try to override activity.finish() like:
#Override
public void finish() {
if(mDialog != null) {
mDialog.dismiss();
}
super.finish();
}
Put the Dialog handle in a member object, then when you finish the top activities, dismiss the dialog first.
You could make this more neat by creating abstract Activity class (which all your activities extends), which dismisses possible dialog when calling finish()

dialog activity control

What I want to do: When the application starts it reads an email address from a database. If the email address is NOT set, it starts another dialog activity for adding the email address.
If the user doesn't add the email, he cannot close that dialog activity, so clicking on close button doesn't close the dialog activity.
The problem that I have is that if the user clicks the back button, the dialog activity closes and the main activity starts without email address set. What I want to do is if the user clicks the back button in this case, to close the application.
I have to mention that I'm using the same dialog activity when I want to edit the email address.
Override onBackPressed, there check whether user has inserted any email address. If not just return without calling super.onBackPressed().
#Override
public void onBackPressed() {
if(yourEmailEditText.getText() == null
|| yourEmailEditText.getText().toString().trim().length() == 0){
return
}
super.onBackPressed();
}
Try Overriding the onBackPressed() method and do nothing into it.
You can set the Dialog as non-cancellable in one of the constructors second parameters:
Dialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener)
Also take into the account what happens if the user by-pass the dialog by any means (any button by hardware, etc). You could simply interprete this as the user wants to exit the application.

how to trigger UI update when alertdialog gets dismissed

I have an AlertDialog which appears initiated by a BroadcastReceiver - so the AlertDialog may appear ontop of ANY of my activities without knowing which activityis actually under it.
private void showAlert(Context context, String s) {
final AlertDialog alertDialog = new AlertDialog.Builder(context)
.create();
alertDialog.setTitle(context.getString(R.string.SMS_Alert_title));
alertDialog.setMessage(s);
alertDialog.setButton(context.getString(R.string.alert_OK),
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return; //can't call the underlying activity's ui update method bec I don't know which activity is actually underlying
}
});
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
}
When I now press the "OK" button on the AlertDialog to dismiss it, the pressed status of all my buttons in the underlying activity become unpressed. I need them to remain pressed simply because my buttons show a different png during pressed status - which I also use to show that they are and remain "ON" when pressed. (I can't use toggle buttons in this case)
Ideally I just need to update my UI of the underlying activity but onResume is NOT called when the AlertDialog gets dismissed.
Also I cannot call any UI update method when pressing the ALertDialog OK button, since I do not know which activity is actually under the AlertDialog (as the ALertDialog may appear ontop of any activity)
(I hope I could explain the problem well enough)
ps While I could change the background of the unpressed buttons to the pressed png instead of just saying btn.setPressed(true) I would like to avoid it.
Many thanks
I'm not quite sure what you're trying to achieve. But have a go at following.
Have an interface named... say 'PressableActivity'? which has one method. 'pressAllButtons'
Implement this on all your activities you want the explained functionality, and implement the method to press all buttons when called.
Have a variable of type 'PressableActivity' on your context (or even a static class and a static variable will do.)
Assign this variable to the activity being displayed when it gets a call to onResume.
When you create the dialog, set an onDismissListener to it, which calls the 'pressAllButtons' method on the object pointed by static variable on your context.
Hope this helps.
One way or another you're going to need to "know" which activity is being displayed after the AlertDialog is dismissed. You can set an onDismissListener on your AlertDialog inside of your Activity and then respond accordingly. Why you would want your buttons to remain in a pressed state after they are not pressed is beyond me, but if that's what you really want then just set the state to pressed since that's essentially what you want: a forced pressed state even though the user hasn't pressed it again.

Alert user if they hit back arrow

I am looking to alert the user if they hit the back button while in the application.. for instance, if the user is half way through using the application and they hit the back arrow, right now it just closes and they would lose all data if they accidentally hit it.
I would like to be able to alert the user with "Do you really want to exit?" so that if it was accidental, they can choose no and continue, and not lose any data.
I'm guessing I will need to implement some sort of listener??
Override onbackpressed() something like...
#Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
builder.setMessage("Do you really want to exit?.").setCancelable(
false).setPositiveButton("Quit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
YourActivity.this.finish();
}
}).setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
if the user is half way through using the application and they hit the back arrow, right now it just closes and they would lose all data if they accidentally hit it.
Then don't lose the data. Save it in onPause(), if not to the permanent data store, to a temporary spot that you then check sometime later.
I would like to be able to alert the user with "Do you really want to exit?" so that if it was accidental, they can choose no and continue, and not lose any data.
Please don't.
This addresses precisely one use case: the user pressing the BACK button. It completely ignores:
the user pressing the HOME button
the user getting a phone call
the user responding to a Notification
the user long-pressing on HOME (or pressing the recent-tasks button in Honeycomb) and switching to another task
etc.
If losing the data is a problem for you when they press BACK, it is a problem for you in all those other cases as well. Hence, handle all the cases, not by interrupting the user when they are trying to leave, but by holding onto the data, then prompting them about the in-flight data if and when they choose to return.
Just override onBackPressed(). One caveat: it's since API 5.
You should override onBackPressed() method of activity and provide logic there.
There are lots of other threads on this. Basically, override onBackPressed()

Categories

Resources