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

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.

Related

Preserve activity stack when notification is clicked

Using just the default way of displaying GCM notifications (and not using onMessageReceived() and GcmListenerService), is it possible to have the activity stack remain as it was and just bring the app to the foreground when a notification is touched? I'd like to get the same behavior as when the app icon is touched. Currently a new activity is launched each time a notification is touched.
Edit:
Here's the situation in detail. I've read that documentation but I'm still not able to get the desired behavior. Let's say my stack looks like this:
Activity B
Activity A
Activity B is currently on top and active. When the user pressed the circle button and then the app icon again, the same stack gets activated (resumed).
Now when touching on a notification in this situation I get this:
Activity A
Activity B
Activity A
But I'd like to just preserve this:
Activity B
Activity A
I've tried all launchMode variants but had no success.
It has more to do with the way your activity is set up. For example, you can make your activity a singleTop activity in your manifest:
<activity
...
android:launchMode="singleTop"
...
/>
This way when there's a new Intent for this activity, Android OS will not make a new instance of your activity, but rather pull up the existing instance and pass the new Intent through the onNewIntent(Intent) function (to make it work, you should override that method in your activity).
There are more ways to do this, check out documentation (the "Using the manifest file" section) to see what's the best fit for your scenario.

how to know if Activity resumed by shortcut or by another top activity

The main activity creates a shortcut to homescreen, all goes good but it needs to know an extra value when it starts by shortcut. So I used this while creating shortcut
intent.putExtra(EXTRA_ID, "Some string info here");
And onResume retrieve value like this
if (getIntent().hasExtra(EXTRA_ID))
ShowToast(getIntent().getExtras().getString(EXTRA_ID));
Everything works fine, lets take some examples
Activity never launched, Home screen shortcut pressed and A toast appears, Nice.
Activity is in background, Home screen shortcut pressed and A toast appears, Nice.
Activity is in use, another activity is launched, when back, toast appears, Not nice.
Activity is in background with no extra, Shortcut pressed, Nothing appears.
In 3rd example, it does what it suppose to do. But how do I know if activity is resumed/launched by shortcut ?
You set an extra in launcher Intent, and want to get the extra when this activity is opened in any state.
The problem you meet:
Activity is in background with no extra, Shortcut pressed, Nothing
appears.
This is because that your activity is started already, the launcher intent just use the existing task as the front task, and its extra data is lost in such situation.
To solve this, you need 2 steps:
Add android:launchMode="singleTask" to this activity in your manifest.
Put the extra data handling codes in both onCreate() and onNewIntent(), not in onResume().
How are you removing the extra EXTRA_ID? You should do it like:
In onResume():
if (getIntent().hasExtra(EXTRA_ID)) {
ShowToast(getIntent().getExtras().getString(EXTRA_ID));
getIntent().removeExtra(EXTRA_ID);
}

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.

Exit application programmatically

I have been searching for an answer but I couldn't find a proper one. The question is that I have a dialog themed activity on top of a normal activity. I would like to force the users to either read and click "OK" to the themed activity which will then transfer them to another dialog themed activity for some further questions or cannot enter the application. So, I would like to exit the application, on back press, and not just finish the themed activity that will reveal the content of my app. How is that possible?
If you start your dialog activity with startActivityForResult() you can send back the result RESULT_CANCELED from the dialog, and upon receiving this (in your main activity) you call finish().
finish() will do the perfect job for you ;)
but make Dialog not cancelable..
Edit (after problem description clarification):
As others said StartActivityForResult could work, with additional trick.
Because of the way you design your App (DialogActivity1->DialogActivity2), it might help to add following line in AndroidManifest file, for all your special dialog-look activities:
android:noHistory="true" or to set flag for intent Intent.FLAG_ACTIVITY_NO_HISTORY before u start DialogActivityN.
Both lines (from manifest or code) will make this actitivies not to stay on android stack, so when your MainActivity get result back, it will be result from last DialogActivity and than depending on result recieved you can either finish() or continue with execution of MainActivity..
when u start activities like this there is no need to call finish() to destroy them, u just start new activity and they will be gone from stack. Of course, in your case, last DialogActivity u will start with StartActivityForResult() and as I explained in previous paragraph MainActivity will do something based on received results.
However making user goes through these dialogs several times at the beginning application, is not something I would consider good practice and it can make your user just give up and go for some less annoying app. (don't get this wrong, it's just my advice to rethink about concept)
Hope you will solve it ;) Cheers
If you know about ActivityforResult then way is easier for you, First you need to start the dialog activity with method startActivityforResult... and then when dialog activity get close by back button you have to close it by Set result. In OnactivityResult method of start activity have to detect the same and close the same if setResult is not as according. Hope you got the point.

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.

Categories

Resources