Dialog Activity also resumes other activities in the background - android

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.

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.

Removing activity from recent apps keeping older one

I have this challenge with my Android APP:
I have and "dialog" - I mean an activity created as a dialog. Now, user presses the home button and leaves the app ~ later holds the home button and displays the Recent apps list. He will see the dialog as the last activity of my app. However, I don't want to display this activity on the "stack".
I tried the exceludeFromRecentApps (or similar name) and this one enabled removes whole app from the list. But I want to have dialog's parent activity displayed in the list.
I tried something with overriding the onResume, onPause and onStop methods, but no effect for me, since instance-level variables may be discarded (?).
I believe thats trivial problem, but I wasn't able to google working solution.
Lot of thanks!
android:noHistory="true"
android:taskAffinity=""
android:excludeFromRecents="true"
for your case, first you need to separate your dialog activity by "taskAffinity" tag. Then you need "excludeFromRecents" tag in you AndroidManifest.xml to remove is from recent list.
Like Hoan Nguyen said, you need to use the finish() method but know that the finish() method does not guarantee that the Activity will be finished.

Android custom layout(alert) in service (similar to messenger)

My question is simply how can i do a dialog pop up from other application.
I search around and many answer is NO.
However, the messenger like Whaxxapp is exactly doing this. It can pop up an instantly reply while i am playing game, taking picture and etc. (like pausing)
And i had do some research and works,
i am able to create a service which start from my activity (activity A).
And the service is registered a contentobserver, when the observer onchange is fired, it can launch an activity(pendingactivity B) and the activity B which i had set the theme transparent and look like a dialog popup. however, it will also change the background back to my activity A and with the popup as well.
What i want to do is not go back to my activity A and just pop up (or launch the pending activity B) when i receive an onchange. (same as Whaxxapp pop up message with interaction)
Thanks so much.
You should start Activity B to new task. So when you press back activity A won't be shown. Add Intent.FLAG_ACTIVITY_NEW_TASK flag to Activity B intent.
I'm guessing, but I think the Whatsapp "Popup" is actually an Activity styled to look like a Dialog.
You can do this changing an Activity's theme to:
<activity android:theme="#android:style/Theme.Dialog" />
You can then add Button's, etc, just like a normal Activity to add interaction.

Avoiding Blank Screen when it Raised from Intent in Android

I have an application that raises from service on scheduled time. In that service, i have displayed my Activity using Intent. It works perfectly. But, when i go back, the activity is finished. But, one blank screen is remaining there? How can i avoid this blank screen. I need my application's Activity instead of blank screen. Anyone Guide me.
#Špĸ remove this line: i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
take a look at my post if you wish to open a dialog via your service: Creating global dialogs
I don't like that you finish an activity that you go back to. You have multiple of different alternatives:
Using fragments instead of activity intents.
Set flag to indicate the activity is done so it will close when you go back.
You could also try to work with the stack, look at this: https://stackoverflow.com/a/4038637/969325
Without seeing the code it is very diffcult to find out what is going wrong in your code but still I am making some effort to help you.
Add android:theme="#android:style/Theme.Translucent" to your activity tag in your manifest file and then try if it works or not.
Have you tried setting FLAG_ACTIVITY_NO_HISTORY in the intent you use to start the AlertDialog activity or launchMode="noHistory" in the manifest.
The AlertDialog activity may very well be the blank screen you're seeing as no content view is set and the only thing a user sees is an alert dialog so once the alert dialog is dismissed and the app goes back to this screen it would be blank.
Before press back button, you have to save the state of the activity and restore the activity while come front. This is easily done by onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() method. Please refer this link.

android dialog opening another view behind it

Hi my app has 3 activities one of which is a dialog. I've done this by using this code in the AndroidManifest.xml
android:theme="#android:style/Theme.Dialog"
the application also has a service which can open the dialog even if the application is not visible, however when it does this it also opens one of the other activities in the background. Is there a way to stop this happening so it opens the dialog in front of the current activity (even if the activity is from a different application)?
Thanks,
ng93
I solved this by adding:
android:taskAffinity=".MyDialog"
to my dialog in the AndroidManifest.xml. Then it is not related to your other tasks and can be shown without bringing up the suspended activity.
You're not able to display something in front of another application's activity. "in front" is somewhat ambiguous, so I'll clarify in that your application can't simultaneously share the screen with another application.
The way I understand it, Android's design inherently requires that when you're doing something on-screen, your app's process/task is in the foreground and in focus, thus bringing the other activities for that task into focus. This implies that other applications are not running with UI focus while your application is in focus.
This means that, for example, when your app launches an activity, it brings your app's stack of activities into focus and puts the new activity (in this case, your dialog box) on top of your app's stack. The result is that if you hit 'back' while displaying the dialog, you'll go back to the activity that was previously displayed. If the stack is cleared of previous activities, 'back' will take you to the application that was previously being displayed on screen.
There are ways of clearing the stack such as: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
Depending on your scenario you might want to clear the stack, or you might "exit" the app when the user closes the dialog. Additionally, the dialog activity could override onBackPressed so that if the user hits 'back' the activity "exits" your app, returning the user to what they were doing before the dialog.
Further reading for activities and stacks: http://developer.android.com/guide/topics/fundamentals.html#acttask
i ended up closing all my apps activities before opening the dialog activity, not ideal but seems like the only way to get the functionality im after

Categories

Resources