开发者

Invoke a dialog from another activity

开发者 https://www.devze.com 2023-02-04 23:41 出处:网络
I have Activity A which defines a dialog using AlertBuilder.create etc.This Activ开发者_Python百科ity invokes the dialog using showDialog(dialogID).dialogID is declared and recognized in all classes/a

I have Activity A which defines a dialog using AlertBuilder.create etc. This Activ开发者_Python百科ity invokes the dialog using showDialog(dialogID). dialogID is declared and recognized in all classes/activities. Everything works perfectly in Activity A.

My question is when trying to invoke this same dialog -- showDialog(dialogID) -- from another Activity (Activity B) the application crashes. Can anyone help?

How to invoke a dialog from multiple activities?

Thanks in advance.

Andy


My question is when trying to invoke this same dialog -- showDialog(dialogID) -- from another Activity (Activity B) the application crashes.

How do you invoke the dialog from Activity B? Just to let you know, you do not instiante activities (so no new ActivityA().showDialog(id).

What you can do is

  1. Create a class that extends AlertDialog.Builder and accept a Context parameter in the constructor. You can customize the text, buttons and other things.
  2. From your activity, in your onCreateDialog, you can just instantiate your class and call create() on it. And your class will be accessible from any activities.


I don't think you can accomplish what you want without hooking up a similar entry point in your new activity.

showDialog(int id)

The id is unique within the activity that launches it. So if two activties A and B both call showDialog(1); That will do something different in each unless someone has coded the same code path for them in their onDialogCreate() and onPrepareDialog methods. So in your onDialogCreate of the original activity, that code will have to exist in both activities. You can sometimes get way with creating a new Dialog type that does all the initialization internally based on a given context and just call show() on it. The problem with this solution usually arises when the context is no longer valid and you need to dismiss or show it. Basically though when using showDialog() it's on a per activity basis.


I don't think that's possible.

Dialog built in Activity A, belongs to Activity A. No matter if you store its ID in a global data space. It won't be available to be used in Activity B. You'll have to create another Dialog in Activity B

0

精彩评论

暂无评论...
验证码 换一张
取 消