Skip to content

Instantly share code, notes, and snippets.

@akexorcist
Created January 6, 2023 11:51
Show Gist options
  • Save akexorcist/3ecbb6a6b03dfb1197bbd432f5a5855d to your computer and use it in GitHub Desktop.
Save akexorcist/3ecbb6a6b03dfb1197bbd432f5a5855d to your computer and use it in GitHub Desktop.
Sample of dialog listener unbinding when device configuration has changed (e.g, dark theme, language, screen orientation)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/buttonShowDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Dialog" />
<TextView
android:id="@+id/textViewDialogAction"
style="@style/TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
tools:text="Dialog result" />
</LinearLayout>
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.lmwn.poc.dialogwithconfigchanges'
compileSdk 32
defaultConfig {
applicationId "com.lmwn.poc.dialogwithconfigchanges"
minSdk 23
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
}
package com.lmwn.poc.dialogwithconfigchanges
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.lmwn.poc.dialogwithconfigchanges.databinding.ActivityMainBinding
import com.lmwn.poc.dialogwithconfigchanges.databinding.ViewAwesomeDialogBinding
class MainActivity : AppCompatActivity() {
private val binding: ActivityMainBinding by lazy {
ActivityMainBinding.inflate(layoutInflater)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.buttonShowDialog.setOnClickListener {
AwesomeDialog.Builder()
.setTitle("Hello World")
.setOnPositiveClick {
binding.textViewDialogAction.text = "Positive"
}
.setOnNegativeClick {
binding.textViewDialogAction.text = "Negative"
}
.build()
.show(supportFragmentManager, null)
}
}
}
class AwesomeDialog : DialogFragment() {
companion object {
private const val EXTRA_TITLE = "extra_title"
}
private lateinit var binding: ViewAwesomeDialogBinding
private var onPositiveClick: (() -> Unit)? = null
private var onNegativeClick: (() -> Unit)? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
binding.textViewTitle.text = arguments?.getString(EXTRA_TITLE)
binding.buttonPositive.setOnClickListener {
onPositiveClick?.invoke()
dismiss()
}
binding.buttonNegative.setOnClickListener {
onNegativeClick?.invoke()
dismiss()
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = ViewAwesomeDialogBinding.inflate(inflater, container, false)
return binding.root
}
fun setOnPositiveClick(action: (() -> Unit)?) {
this.onPositiveClick = action
}
fun setOnNegativeClick(action: (() -> Unit)?) {
this.onNegativeClick = action
}
class Builder {
private var title: String? = null
private var onPositiveClick: (() -> Unit)? = null
private var onNegativeClick: (() -> Unit)? = null
fun setTitle(title: String) = this.apply {
this.title = title
}
fun setOnPositiveClick(action: () -> Unit) = this.apply {
this.onPositiveClick = action
}
fun setOnNegativeClick(action: () -> Unit) = this.apply {
this.onNegativeClick = action
}
fun build() = AwesomeDialog().apply {
arguments = Bundle().apply {
this.putString(EXTRA_TITLE, [email protected])
}
[email protected]([email protected])
[email protected]([email protected])
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="32dp"
tools:context=".MainActivity">
<TextView
style="@style/TextAppearance.AppCompat.Title"
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Title" />
<Button
android:id="@+id/buttonPositive"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="OK" />
<Button
android:id="@+id/buttonNegative"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Cancel" />
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment