Dialog won't 're-show' after pressuing home key and relaunching app - android

I have a 'loading' dialog which is basically a very simple splash-screen that just display a 'loading' graphic while all the resources for my (Android) app loads.
Once it's all done, the dialog is dismissed. Everything works great. Apart from one thing....
If the user presses the 'home' key while the 'loading' dialog is displayed and then returns to the app before everything is loaded, all I get is a blank screen. It still works though.... ie, eventually, the blank screen is replaced by my app.
So, why doesn't my dialog 're-display'? I've confirmed that I'm returning to my app before the dialog is dismissed, so I really don't understand it. At all other points in my game it returns with the screen exactly as it was if paused and immediately relaunched.
I'm creating my dialog like so this code is in onCreate():
load_dialog = new Dialog(MainActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
load_dialog.setCancelable(false);
I then show it like so (also in onCreate())
//Set and display splash screen view
load_dialog.setContentView(splash);
load_dialog.show();
Once everything has loaded, in my GLRenderer classes' onSurfaceCreated() method, I simply dismiss it...
load_dialog.dismiss;
Any ideas?

This is the normal behaviour as your activity is paused when you hit home.
Make your loading_dialog global. Then inside of onResume() call:
if (load_dialog != null && !load_dialog.isShowing())
load_dialog.show();
Also remember that your activity will be re-created even if while loading you rotate the screen. So you need to watch for that too.

Related

Load Content after Activity is visible

I have an activity that loads content from the internet when it initially loads, however the order it takes is:
1) User hits button to go to new activity
2) App stays on old activity
3) App loads content from internet (usually takes about a second or two)
4) App brings user to new activity with content already loaded
Now this is fine, however it can be confusing for a user because while they are still on the original activity, there is no indication that their input did anything. If the user were to click the button again, it wouldn't register that as another click on the original activity, it would register that as a click on the new activity which is still loading, which may cause a user to end up somewhere they did not wish to be.
I'd like to be able to fix this by having the order of the process be:
1) User hits button to go to new activity
2) Old activity disappears, brings user to "blank" new activity without content loaded
3) Display a loading wheel
4) App loads content from internet
5) Content is displayed and loading wheel disappears.
However I can't figure out how to accomplish this, I placed all my code in the onResume() method because I thought that would work, but the content loaded the same way as it always has, does anyone have any suggestions on how to accomplish what I want? Here is the code in my onResume() method:
protected void onResume() {
super.onResume();
setContentView(R.layout.coupon);
//method call to access the URL needed to display the content
accessURL(Global.contentURL);
}
Any help would be greatly appreciated.
You can use an AsyncTask that creates a ProgressDialog while it fetches the data in the background. There are many questions about this on SO (e.g. How to use AsyncTask to show a ProgressDialog while doing background work in Android?) as well as on the Internet.

android: dim the screen when application is in background

I am developing a small app which shows passwords of the user through a Dialog screen.
When home button is pressed, I need to dim the screen (on the multi tasking window) so that any other person cannot see the password.
When user re-opens the app, it asks an application lock. But if the user leaves the password Dialog open and presses the home button, dialog and the password which user last looked at stays visible (on the multi tasking window) for a while (3-4 seconds!!) until a new dialog asks the lock.
So far I tried ever possible dialog.dissmiss() options. Dialog dismisses only when app is opened again (until a new lock dialog appears) even I put dismiss() in onPause, onStop etc.
Any idea appreciated.
I also tried,
android.os.Process.killProcess(android.os.Process.myPid());
this.finish();
System.exit(0);
none of them actually worked.
Suggestion 1: Double-check your implementation. Tying your dialog to the activity lifecycle seems like a good idea (especially to avoid leaked window errors as described here)
The following example works out well for me (with coachMark being derived from Dialog)
#Override
protected void onResume()
{
log.debug("onResume");
super.onResume();
// Show the coachMark depending on saved preference values
coachMark.mayBeShow();
}
#Override
protected void onPause()
{
log.debug("onPause");
// Hide the coachMark if it is showing to avoid leakedWindow errors
coachMark.maybeHide();
super.onPause();
}
onPause definately gets called when you press the home button, so if this approach does not work for you, try not recreating the dialog in the restarting part of the acitivty lifecycle (onRestart(), onStart() and onResume()) and see, if it gets dismissed correctly.
Suggestion 2: Should all of the above fail, you might consider overriding the home button as described here. I highly advise against it though, since this may cause the app to work in an way that the user does not expect it to.

Dialog is re-presented after onPause()->removeDialog() - why?

I'm just not getting why this is happening:
Successfully create dialog and instantiate it when user clicks on an item. Zero problems getting dialog to show when desired and to go away when back is pressed....
The problem is:
If the dialog is still displaying when the application/activity goes away (pause, die), when it returns, the dialog is still presented but no variables which are context specific are presented. Note: I do issue removeDialog() for the dialog in the onPause(). Yet mystically when the app returns, it's somehow cycled thru onPrepareDialog() and loaded up with missing variables (xml names show).
What I want is for the dialog to go away and let the user make a different choice from the main activity and then redisplay the dialog - if that's what they want. Or said another way - I want dialog to always go away if the app goes away.
I've looked into persisting the data but there doesn't seem to be a "right" spot in the restore process to do that (or I'm just ignorant). I can't figure out why onPrepareDialog is getting invoked during onResume()....

AlertDialog disappears after switching activity

I have an android application having an AlertDialog with OK and Cancel buttons. When the dialog shows without pressing the OK or Cancel button, just press Home button of device. The Home screen will show. Now open another application suppose Camera. Take some picture or Video. Now get out from the Camera application. Now open my android application and surprisingly the alertdialog have disappeared. Why?
I'm guessing you are creating this AlertDialog onCreate() method.
First, you should read up on the Activity Lifecycle.
And what happens is that when you go to another app, the Activity goes to onPause method, which cleans up a bit.
Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.
Then because you return to the app, it calls the onResume method, which doesn't create your dialog again.
If you want to show dialog on startup of application then write this code in
onResume()
method, it will show dialog every time when user returns to this screen.
Or you can manage its state in
onPause()

How to handle feedback from background threads if the user uses the back button?

I have the following problem:
I have an Activity where a user can start a web search showing a new activity to show a progress bar until the results are shown. Now the user can either wait for the results or maybe think about the search parameters, hit the back button and triggering a new search. The search is running in an Async Task and therefor still running if the user hits back.
At the moment the thread finishes it calls some methods on the old activity causing the activity to show a dialog.
This causes the system to crash because the dialog tries to show itself with a reference to an activity that is not longer present on the screen.
How can I achieve a dialog that is only shown if the activity is still active?
Call isFinishing() on your activity.

Categories

Resources