Andy
Andy

Reputation: 21

Invoke a dialog from another activity

I have Activity A which defines a dialog using AlertBuilder.create etc. This Activity 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

Upvotes: 1

Views: 1260

Answers (3)

Aman Aalam
Aman Aalam

Reputation: 11251

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

Upvotes: 0

ccheneson
ccheneson

Reputation: 49410

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.

Upvotes: 1

Greg Giacovelli
Greg Giacovelli

Reputation: 10184

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.

Upvotes: 0

Related Questions