An Activity can use dialogs to organize information and react to user-driven events. For example, an activity might display a dialog informing the user of a problem or ask the user to confirm an action such as deleting a data record. Using dialogs for simple tasks helps keep the number of application activities manageable.
Exploring the Different Types of Dialogs
There are a number of different dialog types available within the Android SDK. Each has a special function that most users should be somewhat familiar with. The dialog types available include
The different dialog types available in Android.
If none of the existing Dialog types is adequate, you can also create custom Dialog windows, with your specific layout requirements.
Tracing the Lifecycle of a Dialog
Each Dialog must be defined within the Activity in which it is used. A Dialog may be launched once, or used repeatedly. Understanding how an Activity manages the Dialog lifecycle is important to implementing a Dialog correctly. Let’s look at the key methods that an Activity must use to manage a Dialog:
Adding the Dialog to an Activity involves several steps:
Defining a Dialog
A Dialog used by an Activity must be defined in advance. Each Dialog has a special identifier (an integer).When the showDialog() method is called, you pass in this identifier. At this point, the onCreateDialog() method is called and must return a Dialog of the appropriate type.
It is up to the developer to override the onCreateDialog() method of the Activity and return the appropriate Dialog for a given identifier. If an Activity has multiple Dialog windows, the onCreateDialog() method generally contains a switch statement to return the appropriate Dialog based on the incoming parameter—the Dialog identifier.
Initializing a Dialog
Because a Dialog is often kept around by the Activity in its Dialog pool, it might be important to re-initialize a Dialog each time it is shown, instead of just when it is created the first time. For this purpose, you can override the onPrepareDialog() method of the Activity.
Although the onCreateDialog() method may only be called once for initial Dialog creation, the onPrepareDialog() method is called each time the showDialog() method is called, giving the Activity a chance to modify the Dialog before it is shown to the user.
Launching a Dialog
You can display any Dialog defined within an Activity by calling its showDialog() method and passing it a valid Dialog identifier—one that will be recognized by the< onCreateDialog() method.
Dismissing a Dialog
Most types of dialogs have automatic dismissal circumstances. However, if you want to force a Dialog to be dismissed, simply call the dismissDialog() method and pass in the Dialog identifier.
Removing a Dialog from Use
Dismissing a Dialog does not destroy it. If the Dialog is shown again, its cached contents are redisplayed. If you want to force an Activity to remove a Dialog from its pool and not use it again, you can call the removeDialog() method, passing in the valid Dialog identifier.
Working with Custom Dialogs
When the dialog types do not suit your purpose exactly, you can create a custom Dialog. One easy way to create a custom Dialog is to begin with an AlertDialog and use an AlertDialog.Builder class to override its default layout. In order to create a custom Dialog this way, the following steps must be performed:
Setting Up Your Android Development Environment
Writing Your First Android Application
Understanding The Anatomy Of An Android Application
Defining Your Application Using The Android Manifest File
Managing Application Resources
Exploring User Interface Screen Elements
Designing User Interfaces With Layouts
Drawing And Working With Animation
Using Android Data And Storage Apis
Sharing Data Between Applications With Content Providers
Using Android Networking Apis
Using Android Web Apis
Using Location-based Services (lbs) Apis
Using Android Multimedia Apis
Using Android Telephony Apis
Using Android 3d Graphics With Opengl Es
Using The Android Ndk
Using Android’s Optional Hardware Apis
Working With Notifications
Working With Services
Extending Android Application Reach
Managing User Accounts And Synchronizing User Data
Handling Advanced User Input
Targeting Different Device Configurations And Languages
The Mobile Software Development Process
Designing And Developing Bulletproof Android Applications
Testing Android Applications
Selling Your Android Application
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.