I\'ve got an app that responds to an Alert - and I really need to show an alert with a password entry before going on to the next screen; the trouble is I don\'t seem to be able
Create an activity, using Theme.Dialog
, and call startActivity()
to open it. While you cannot use a "real" Dialog
, you can create a UI that looks like a Dialog
.
Bear in mind that popping up an activity based on a broadcast may greatly irritate some users, who might not appreciate your dialog appearing in the middle of their game or text message conversation or whatever. You may wish to consider actually raising a Notification
, then displaying the dialog when they open up that Notification
.
Go through the link below this is the complete example how to make a custom dialog and call it automatically and gets pop up when the internet gets down hope it works for you
Click Here
You can't open Dialog from Receivers because it need ActivityContext
Alternate way You can open Acivity like dialog. Full example :
MyReceiver.kt
class AlarmReceiver : BroadcastReceiver {
private val REMINDER_BUNDLE = "MyReminderBundle"
override fun onReceive(context: Context?, intent: Intent?) {
val mIntent = Intent(context, DialogActivity::class.java)
mIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context?.startActivity(mIntent)
}
}
AndroidManife.xml
<activity android:name=".appview.settings.view.DialogActivity" android:excludeFromRecents="true" android:theme="@style/Theme.AppCompat.Dialog"/>
DialogActivity.kt
class DialogActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dialog)
this.setFinishOnTouchOutside(true)
// create custom view of your dialog in activity_dialog
// or you can direct call alert dialog
}
}