Android Activity.startActivity() and Dialog.show() order - android

I need to show a EULA like dialog when a user starts up my app, but my app also has a splash screen of sorts that needs to be displayed before the dialog. I coded this up like so:
Activity A.onCreate(...) {
...
1. startActivity(Activity B) [this activity calls .finish() after x seconds]
2. dialog.show()
...
}
Logically, it seems like this should work. However, after I return to activity A from activity B, the entire screen is dimmed as if the dialog is showing, but without the dialog window.
I was able to work around this by reversing the calls as such and the dialog shows after activity B is done and no weird issues occur:
Activity A.onCreate(...) {
...
1. dialog.show()
2. startActivity(Activity B) [this activity calls .finish() after x seconds]
...
}
Anyone know why the order of calls is so important? To me they should work the same.

Both of those functions are asynchronous functions. This means that the code execution does not pause on them, but continues on to call the next lines.
Instead of calling them one right after the other, you need to wait for the one to return before calling the next. If you want to show the Dialog first, add an onDismissListener to listen for when the dialog is closed. Within that listener you can start the next activity.

Related

Launching a dialog from a widget does not work properly

I have a widget that launches a dialog with two options. One Button to make emergency call, and another to call customer service. Once the dialog is launched from a widget, and I tap on one of the two option, the button doesn't respond. But, if I background the app and bring it to foreground, then that previous selection of the button that I made gets called. I'm using the correct flag when launching the activity from the Widget.
The order of the lifecycle of the Fragment that takes place when things are working normally with the Dialog is below. The similar lifecycle takes place when foreground and backgrounding the app. Not exactly sure why the callback for the buttons on the Dialog doesn't respond when launched from the Widget. Thank you!
OnCreateView()
OnViewCreated()
OnStart()
OnResume()
I was able to fix this problem by simply using navController.navigate(). This allows the NavController to handle the Fragment lifecycle properly and in the correct order.

Dialog Activity also resumes other activities in the background

This is the scenario. I have 2 activities in my application and a Dialog activity that is started when I click on the notification created by my application.
The problem is that when I click on the notification, only the Dialog should show, not the other activity of my app, if it was stopped on pressing the home button.
When I close my application by pressing the back button, the dialog activity shows the dialog, but when the application is running in the background, that activity also opens up on creating the dialog activity.
I use #android:style/Theme.Dialog for my dialog activity.
How to only show the Dialog activity, not other activities in the backgroud?
The solution to the OP issue is setting a different Task affinity for that activity in the Manifest.
<activity android:name="MyIndependentActivity"
android:theme="#android:style/Theme.Dialog"
android:taskAffinity="a_unique_id">
The taskAffinity string must be different from the package name (com.whatever.myapp)
I also use android:excludeFromRecents="true", to hide the dialog from the recent apps list, as it usually makes no sense returning to a dialog.
More info: http://developer.android.com/guide/components/tasks-and-back-stack.html#Affinities
Yeah that'll happen.
When you declare a theme of Dialog it affects the activity lifecycle and the previous activity doesn't go into onStop so some Android functions still think it's the active activity which is technially true as your dialog Activity is acting like a dialog.
One possible work around if you don't 'care' that you can see the previous activity behind the dialog is to change the dialog to be a DialogFragment, put the theme of dialog on the fragment and show this in it's own activity, that'll do it.
The way I have done this (and this can't be the best way to do this) is to have some logic in the activity.onPause() and activity.onResume() methods, that will perform actions based on what I want. My experience is more around separate activities and transitioning between them than using dialogs alot.
You can pass information between activities through setResult(). This will enable you to work out why the child activity has decided to close. That combined with the onResume function should enable you to disable the parent activity.
To override the dialog so that the other activity is not visible behind it, is probably to use the onPause() method to make it go translucent.
I have found onStop() very irratic to use and often unnecessary. The reason for this, is that it is called unpredictably from a developers point of view because onStop can be called based at strange times based on whether the OS has enough memory etc. onPause however in my experience is always called predicably.

how to make an alert box display over other activities

Suppose i am having an activity (let's say activity1) and i have given a command for doing some long process. Mean while i am starting another activity (activity2), during this time if the activity1 finishes the process and shows the result in an alert box, then how can i make this alert box of activity1 appear over activity2? What i have noticed is that, the alert box of activity1 is only visible when i move back to activity1. Is there any way to do this? Ignore if the question is irrelevant, as i am just a beginner in android.
Before using two activity, you have to save the state of the activity and restore the another activity while come front. This is easily done by onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() method. Please refer this link.

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()

Does pressing Back always cause Activity to finish()?

I've heard that pressing the back button will essentially cause the current Activity to finish(). Is this always the case? Seems like it would be with the way it pops the Activity off the stack.
The one situation I'm not so sure about is when the root Activity in a Task has back pressed. I'm currently experiencing a very weird effect, described as follows:
On loading my application, the first Activity is for initialization, and once it finishes, it calls my main Activity (a TabActivity). This first init activity has android:noHistory="true" set in the Manifest so pressing Back from my main Activity won't go back to that. It goes to the Launcher. When I click on my App in the Launcher a second time, the initialization activity loads again, and loads the main Activity when done. Almost immediately after, it loads a second instance of my main Activity. But ONLY after the Application has already been run once, and was exited by pressing BACK from the main Activity. It does it every subsequent time until I force quit the app or load a new version from the IDE.
Based on this, I am suspecting some kind of Activity instance is lying around and being reused, since it only happens on the second+ time I run the application (and exit with BACK -- using HOME just returns to the last state of the app, no big deal). Anyone have any thoughts??
I've heard that pressing the back button will essentially cause the current Activity to finish(). Is this always the case?
No it is not. The most activities have this behaviour but not all. For example you could create a Dialog and set it setCancelable(false) and it won't close if you click BACK button.
Furthermore you could customize activity behaviour on BACK button pressed by overriding onBackPressed
Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.
About your application behaviour..Did you verify if the activity launcher is finished after it loads your main activity? I mean if the onDestroy() method is called. Maybe after it runs the main activity it remains there and when you click back you just go back to the old Launcher...
hope this helps..
Read through the Activity and Task design guidelines on the Android developer site; they explain how the Home and Back buttons work. Obviously, if you override the default behavior (as mentioned by hara above), the back button will not finish the activity.
On your specific issue, check your logcat. You should be able to see there whether it is bringing an old process back to life or starting up a new one. If that is unclear, insert a couple of log statements into onCreate, onPause, onDestroyed, etc., so that you can see exactly what is happening with your process.
You can control BACK-BUTTON by writing the following code.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK ) {
//preventing default implementation previous to
//android.os.Build.VERSION_CODES.ECLAIR
return false;
}
return super.onKeyDown(keyCode, event);
}
Are you running your activities with any special flags, such as singleInstance or singleTop? Those could be causing the oddities you're seeing. The easiest way to track down what's causing your problem is to absolutely fill it with debugging messages. For example:
In your initialisation activity, add a log in the beginning of onCreate to get the name of the activity such as this.toString(). More on why you want this line later.
When it launches the main tabbed activity, get the name of the launching activity and a message saying it's launched the tabbed one.
Override the onPause(), onStop() and onDestroy() callbacks and add debugging lines with this.toString() and also a message telling you which callback it is.
What this will do is tell you whether you've got multiple instances of the initialisation activity lying around. To this by comparing the name of the activities calling your main activity with the ones that were just created and the ones that went through to onDestroy.
If you don't know how to debug, use Log.d(LOG_TAG, "Your message here");. And then define a constant LOG_TAG String somewhere. After that, show the LogCat perspective in Eclispe by going to Window, show perspective (or view, don't remember exactly), other, Android, LogCat. The purpose of having a LOG_TAG constant is that you can set up LogCat to filter to that String and only show you those messages. It will make it easier to see them among the mass of system log messages.
The short answer to the original question is 'no'. This is largely because, unfortunately, not every developer follows the guidelines referenced by previous answers.
Yet the guidleines themselves mention exceptions, when the Back key should not call finish(). the most prominent exception is the Web browser, which has its own "back stack" for each window, so it must have its own custom handling of the Back key.
If there are no fragments on the back stack and a developer has not overridden onBackPressed, the activity will finish when the back button is pressed.
Here is the source code for Android 4.4.2 Activity.onBackPressed():
public void onBackPressed() {
if (!mFragments.popBackStackImmediate()) {
finish();
}
}
just override onbackpressed().. on back press this method will get execute remove super and do what u want to do.

Categories

Resources