Reputation: 3273
I have to show a Dialog in the foreground activity as a result of a background http operation.
When the dialog has to come up it could be everywhere, the context can be changed, for example I have started a new Activity.
If I use the applicationContext to show the dialog I get:
05-04 17:32:32.560: E/AndroidRuntime(3663): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
so... How can I achieve my aim?
any suggestions?
Upvotes: 2
Views: 2288
Reputation:
In my opinion the best way is creating one new activity and using it like a dialog. The steps:
Create new activity , for example (to being original) MainActivy.
Fill the activity_main.xml asociated as you need.
In the your AndroidManifest.xml re-write this lines.
<activity
android:theme="@style/AppTheme.Dialog"
android:name="com.myapp.original.example"
android:label="@string/timy_titlle" >
Call your MainActivity, now converted to Dialog, from others activies using the Intent class.
If you are using Action Bar Compact you can follow this others steps.
Upvotes: 0
Reputation: 4784
You need a way to notify your foreground Activity that the operation is complete, you can do this by registering a listener, since you have not posted any code, I will make assumptions.
There are two ways you can notify a foreground Activity that I know of, the first way is using broadcast intents, there is a question here relating to them Android BroadcastReceiver within Activity. You can fire of a broadcast intent from your background operation and register your activity as a receiver.
See here http://developer.android.com/reference/android/content/Context.html#sendOrderedBroadcast%28android.content.Intent,%20java.lang.String,%20android.content.BroadcastReceiver,%20android.os.Handler,%20int,%20java.lang.String,%20android.os.Bundle%29 and here http://developer.android.com/reference/android/content/BroadcastReceiver.html
The second way is to register a listener with your class that performs the background operation, for instance (pseudo code)
@Override
protected void onResume() {
BackgroundOperator.registerListener(this);
}
@Override
protected void onPause() {
BackgroundOperator.unregisterListener(this);
}
public void onOperationComplete(...) {
// TODO: Show your dialog here
}
Where your listener could be something like this (which your Activity could implement):
interface BackgroundOperatorListener {
void onOperationComplete(...);
}
The idea here is your foreground activity will be the current registered listener, so it will be the recipient of the onOperationComplete(...) callback, and you can then show your dialog, the ... could be any number of arguments to pass to your activity when the operation is complete.
Upvotes: 0
Reputation: 2913
I think what you need is get the top activity of the task(current showing activity), then use it to show dialog.
so see this thread :How to get any identifier of the topmost activity?
Edit: Showing a dialog from background is not a good user experience, you can send notification or just make a long time toast.
Upvotes: 0
Reputation: 2101
Whenever/wherever you create the dialog you would be in an activity right? Why not just use that activity as the context?
In my own code, I create a helper class that creates a dialog for me. Into that helper class, I pass in the current activity, title and message. It constructs the dialog and returns an AlertDialog object which I can manage.
You could try that, but you would still need to know the context/activity where you want your dialog to display.
Upvotes: 2