2

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Noodles
  • 3,263
  • 4
  • 19
  • 22
  • Here is a similar question: [PopUp dialog Android from background thread](http://stackoverflow.com/questions/1027149/popup-dialog-android-from-background-thread) – Sam May 04 '12 at 16:00
  • Have you seen my answer? – dreamtale May 08 '12 at 15:43

4 Answers4

2

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.

Gophermofur
  • 2,101
  • 1
  • 14
  • 14
  • 1
    No, I can't because while I'm waiting for background result (using handler) I can start a new activity changing the current context. The handler of the previous activity has be able to show the dialog not in the activity where the handler is (your solution) but in the new one. – Noodles May 04 '12 at 16:03
0

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.

Community
  • 1
  • 1
Ian Warwick
  • 4,774
  • 3
  • 27
  • 27
  • the problem is... how to know which is the foreground activity? – Noodles May 04 '12 at 16:34
  • ok, I have updated my answer so activities register in onResume and onPause, this will ensure that only the foreground activity gets the callback from the background operation, hope it helps. – Ian Warwick May 04 '12 at 22:38
  • By the way, if you are making web requests I highly recommend looking at Virgil Dobjanschi's Google IO REST presentation http://www.youtube.com/watch?v=xHXn3Kg2IQE he describes a similar method (in regard to his Service Helper class) – Ian Warwick May 08 '12 at 06:41
0

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.

Community
  • 1
  • 1
dreamtale
  • 2,905
  • 1
  • 26
  • 38
0

In my opinion the best way is creating one new activity and using it like a dialog. The steps:

  1. Create new activity , for example (to being original) MainActivy.

  2. Fill the activity_main.xml asociated as you need.

  3. 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" >
    
  4. 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.

Marcel Gwerder
  • 8,353
  • 5
  • 35
  • 60