#Override
protected void onStop() {
super.onStop();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Test message")
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
This obviously isn't working, as soon as dialog is shown - activity is stopped (dialog disappears). How to solve this issuse?
I want to save some setting into my database as soon as activity is left (via back button, clicking on some button which leads to some other activity, click on notification and so on..) and then show the result in AlertDialog?
Or even better - when Android recognizes that activity will be closed, it saves my settings, show AlertDialog and then onClick Activity is finally closed.
It's a very bad practice to try to do something that takes time and user attention in onStop and onPause. Usually these methods are used to save some data. You can try to show a Toast, but the best way - is to show nothing, as it's not an usual practice. Is there really something important you need to show? This is the question you must resolve in first place.
You should call super.onStop() after you are done with your processing.
I guess you need to send settings when the user exits the application or application envokes onPause(). Why don't you try to grasp all ways of exiting application lets say Back button pressed, so add listener for all these events. And do the sending.
Related
I am using this link 1 to detect the swiping right on my main activity and using this link 2 to put the alertbuilder in the event Handler.
Link 1: Android: How to handle right to left swipe gestures
Link2: How can I put "Are you sure you want to exit?" when I press back button android
But I get an error
E/WindowManager: android.view.WindowLeaked: Activity de.dfki.av.mhs.peripheralwatch.MainActivity has leaked window DecorView#334a170[MainActivity] that was originally added here
How to add a confirmation screen for the user before he exits the app?
if you followed that steps then you have implemented the onbackpressed of activity but be aware dont call super.onbackpressed before the dialog.show().
Also be sure to call finish() on button click of dialog instead of onbackpress.
Error is saying that you are showing dialog after exiting the activity.
#Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ExampleActivity.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
I have this scenario where I have to display an AlertDialog when the current running Activity is brought to front.
It is displayed only when the current running activity is the first in the Activity stack and otherwise won't.
My hunch is this is related to current Context which is passed to create the AlertDialog.
Any suggestions?
--EDIT--
The code looks something like this, the alert dialog creation as usual.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
The above image describes a lot. when an activity launched, At the very first and only once it calls onCreate() method. Then it calls onStart() and so on.
Your question is no clear.
I am supposing that you launched an activity that contains the alertDialog when you launch the activity first time the onCreate() method will be called. And if the alertDialog is called in this method then it will be popped up. But if you start another activity without destroying the same then the activity will go to the background but it is still running(in onPause() mode). When you finish the new activity and come back to the targeted activity it will now call the onResume() method not onCreate() because the method is not needed as the activity is already created. But it will definitely be called after destroying the activity. So you may use onResume() to call the alertDialog after coming back from another activity.
Apply AlertDialog code in OnResume() method and onStart() method also and you'll be good to go.
Good Luck :)
In case of me your code for dialog is not show me a dialog box, i change the code and run it was running properly,
i think this code help to you.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Title");
alertDialog.setMessage("MEssage");
// AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
// AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
I want to know if when the user presses "Yes" on an alert dialog and this one is dismissed, that event executes the onResume method of the activity in which the user is in.
Because I have a "Clean" button that asks the user if he's really sure of cleaning all the fields of the form (the activity) in order to redraw the activity with the empty fields.. The form is created dynamically, so I don't know a priori the elements in the GUI to set them empty...
Sorry for my bad english!!
Thanks and Greetings!
Not sure if this is the approach that should be taken, but you should be able to do what I think you are requesting. If for some reason this is what you would want to achieve.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to clean?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.dismiss();
((ActivityName) appContext).onResume();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.dismiss();
}
});
builder.create().show();
}
You will really want to be calling your clean function instead of anything like a lifecycle call on a success while doing nothing on a failure.
Another potential way to approach this would be to bring the current activity back to the front using flags.
Intent intent = new Intent(this, CurrentlyRunningActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Which would also provide a way to call upon your main activity without directly referencing the onResume() call, as pointed out is not the appropriate approach; however, I did want to directly respond to the question as it was asked.
To see if a method is called you can put a breakpoint at the method, onResume(), to see what happens. If you aren't familiar with the Acitvity Lifecycle then doing this will help you familiarize yourself with it and reading the provided documentation.
Now, I don't think you should redraw your whole layout just to clear some Views. It would be more efficient, in my opinion, to just reset all fields by using setText() or another method for whatever you need when the user clicks "ok " or whatever. You can use invalidate() if you need to redraw certain Views
I also recommend watching
Google I/O-Turbo Charge Your UI
Activity LifeCycle <-- very important to understand
AFAIK This is not possible since in order to have displayed the dialog box the activity would have already passed the onResume state. Check out the following page for more on the life cycle on an Android app (it really helped me understand better):
App lifecycle
If you're not sure when the onResume is called, add a log into the onResume method.
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.
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()