Custom Dialog With Data Passing Using Bundle

This example will explain how a custom dialog is create in onCreateDialog(). The text in the EditText will be passed to the custom dialog via a bundle in onPrepareDialog(int, Dialog, Bundle).

If you use showDialog(int), the activity will call onCreateDialog() method the first time, and hang onto it thereafter. Any dialog that is created by this method will automatically be saved and restored for you, including whether it is showing. If you would like an opportunity to prepare your dialog before it is shown, override onPrepareDialog(int, Dialog, Bundle). We will pass the text from main activity to our custom dialog, so we have to implement onPrepareDialog(int, Dialog, Bundle). And the text will be passed to dialog in bundle.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it CustomDialogWithBundle.

2.) Write following into main.xml:

 

 

 

3.) Create and write following into dialoglayout.xml:

 

 

4.) Run for output.

Steps:

1.) Create a project named CustomDialogWithBundle and set the information as stated in the image.

Build Target: Android 4.4
Application Name: CustomDialogWithBundle
Package Name: com.example.CustomDialogWithBundle
Activity Name: CustomDialogWithBundleActivity

CustomDialogWithBundle1

2.) Open CustomDialogWithBundleActivity.java file and write following code there:

 

 

 

3.) Compile and build the project.

Output

CustomDialogWithBundle2

CustomDialogWithBundle3