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.
Related
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.
I want to add a particular activity (suppose ask password) when user opens any application. How can i do this in android?
-For example: Suppose user wants to open camera, it should be redirected first to that password screen, user will add password and if it is right he would be able to open camera.
For that you need to declare a background service to capture the event when camera application is opened by the user. Background service will have onReceive() method. In that method, you can get the list of running applications processes and match that if the camera is in the foreground. If the camera is clicked by the user, it will appear in the list of the running applications. Then you intercept that event and show the dialog fragment you intend to show to the user.
You can implement a custom DialogFragment class which has required field for asking user the password.
In the Custom dialog class you declare an interface, something like this:
public interface CustomAddressDialogListener{
public void onDialogPositiveClick(DialogFragment dialog, String enteredPassword);
public void onDialogNegativeClick(DialogFragment dialog);
}
Custom DialogFragment will have its own layout for entering the password and you can set Positive actions and negative actions.
Positive action: user enters password and hits submit.
Negative action: user doesn't want to enter password and clicks cancel.
Your parent activity can implement this interface to capture positive and negative click.
#Override
public void onDialogPositiveClick(DialogFragment dialog, String enteredPassword) {
// Here you can check if the password is correct or wrong and take the action accordingly to open the camera.
}
While handling the negative action you can kill the camera process using kill the camera process: android.os.Process.sendSignal(pid, android.os.Process.SIGNAL_KILL);
Here pid is the process id of the camera application. You can obtain pid when you obtains information of running application processes.
For details you can refer to: http://developer.android.com/reference/android/app/DialogFragment.html
In my app, the user logs in with a custom made login dialog. The user can confirm and exit the dialog in two ways:
Press the Enter/Done button in the password box.
Press the OK button.
When the user has confirmed, the provided credentials are verified. If the credentials were incorrect, the dialog will reappear. This does only work if the user presses the Enter/Done button in the password box and not if the OK button is pressed. I use the same code for both the TextView.OnEditorActionListener and the DialogInterface.OnClickListener. I've tried debugging the code and I've discovered that in both cases, the boolean android.app.Activity.showDialog(int id, Bundle args) return true, which tells if the dialog was displayed or not.
I believe your best bet is to just create a new dialog. Should'nt be to hard.
UPDATE:
Also you could set it to
setVisibility(View.GONE);
And then when you want it to be show again
View.VISIBLE
Why don't you just hide() it? Only dismiss() it when you are really done with the Dialog
My program has a email function. There are two activities, A and B. The email function is in activity B. When I click email button, it will pop up a dialog to let user to choose whose to send. then will call email.
The problem is, every time when I click send, it will be return to the dialog(choose list). Is there anyway to let the program return to activity A automatilcally when user click send button. I can't do a onclick listerner becase email system is out of my control.
thanks.
in the on click function of the dialogue that you have implemented try calling dialog.dismiss() before switching to Activity B.
You can finish activity b manually by calling finish()
Friends i have an application with an Activitiy which brings some data from Content Providers and Display it in the textViews and edittext onto the Screen. but before that it prompts me for the username and password in a dialog. i have done all the getting content Providers Stuff in the positiveButton onClick Listener of the Alert Dialog.
It works fine but problem is that if i dont enter username and just press bakc key button it closses down the Dialog Box and the Back Screen is showed without loading the content providers.
Note: I have put my code of alert dialog in the onCreate Function of that activity.
So can u guide me how should i do it that when i press back key on dialog box it also should not display my activity.
Please Help!
your dialog name here.setCancelable(false);
this is working
I would honestly just do:
mDialog.setCancelable(false); //assuming the field mDialog is your Dialog
Then make sure you have both an Okay and Cancel (Positive and Negative) buttons on your Dialog. This way, the back press will do nothing, and you can use the Cancel button to finish your activity as well if desired.
The Dialog interface provides a setCancelable() method which enables precisely this. You call that with a false value and the user won't be able to press the back button to go back to your activity.
You can use method of each activity is : onBackPressed()
http://developer.android.com/reference/android/app/Activity.html
-This is the method which you can override sub method to execute when user enter back key.