Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.

Commit a1a5d66

Browse files
authored
Merge pull request #95 from amardeshbd/69_update_leak_canary
[FIXED] [#69] Memory leak for the dialog.
2 parents 498690b + 510616e commit a1a5d66

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

app/src/main/java/com/hossainkhan/android/demo/ui/dialog/LayoutInfoDialog.kt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@
1616

1717
package com.hossainkhan.android.demo.ui.dialog
1818

19+
import android.net.Uri
1920
import android.os.Bundle
2021
import android.view.LayoutInflater
2122
import android.view.View
2223
import android.view.ViewGroup
2324
import android.widget.FrameLayout
2425
import android.widget.TextView
26+
import androidx.browser.customtabs.CustomTabsIntent
2527
import com.google.android.material.bottomsheet.BottomSheetBehavior
2628
import com.google.android.material.bottomsheet.BottomSheetDialog
2729
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
2830
import com.google.android.material.button.MaterialButton
2931
import com.hossainkhan.android.demo.R
32+
import timber.log.Timber
3033

3134

3235
/**
@@ -36,11 +39,13 @@ class LayoutInfoDialog : BottomSheetDialogFragment() {
3639
companion object {
3740
private const val BUNDLE_ARG_KEY_TITLE = "BUNDLE_TITLE"
3841
private const val BUNDLE_ARG_KEY_DESC = "BUNDLE_DESCRIPTION"
42+
private const val BUNDLE_ARG_KEY_LAYOUT_URL = "BUNDLE_LAYOUT_URL"
3943

40-
fun newInstance(title: String, description: String): LayoutInfoDialog {
44+
fun newInstance(title: String, description: String, layoutUrl: String): LayoutInfoDialog {
4145
val args = Bundle()
4246
args.putString(BUNDLE_ARG_KEY_TITLE, title)
4347
args.putString(BUNDLE_ARG_KEY_DESC, description)
48+
args.putString(BUNDLE_ARG_KEY_LAYOUT_URL, layoutUrl)
4449

4550
val dialog = LayoutInfoDialog()
4651

@@ -55,7 +60,6 @@ class LayoutInfoDialog : BottomSheetDialogFragment() {
5560
lateinit var infoDescription: TextView
5661
lateinit var okButton: MaterialButton
5762
lateinit var previewXml: MaterialButton
58-
var previewXmlListener: (() -> Unit)? = null
5963

6064
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
6165
val view = inflater.inflate(R.layout.dialog_layout_info_sheet, container, false)
@@ -83,6 +87,7 @@ class LayoutInfoDialog : BottomSheetDialogFragment() {
8387
arguments!!.getString(BUNDLE_ARG_KEY_TITLE, ""),
8488
arguments!!.getString(BUNDLE_ARG_KEY_DESC, "")
8589
)
90+
8691
}
8792

8893
override fun onPause() {
@@ -98,7 +103,19 @@ class LayoutInfoDialog : BottomSheetDialogFragment() {
98103
okButton.setOnClickListener { dismiss() }
99104
previewXml.setOnClickListener {
100105
dismiss()
101-
previewXmlListener?.invoke()
106+
loadLayoutUrl(arguments!!.getString(BUNDLE_ARG_KEY_LAYOUT_URL, null))
102107
}
103108
}
109+
110+
/**
111+
* Loads currently running layout from Github into chrome web view.
112+
*/
113+
fun loadLayoutUrl(url: String) {
114+
Timber.d("Showing layout source code via: %s", url)
115+
val builder = CustomTabsIntent.Builder()
116+
builder.setShowTitle(false)
117+
.addDefaultShareMenuItem()
118+
val customTabsIntent = builder.build()
119+
customTabsIntent.launchUrl(context, Uri.parse(url))
120+
}
104121
}

app/src/main/java/com/hossainkhan/android/demo/ui/layoutpreview/LayoutPreviewBaseActivity.kt

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,15 @@ open class LayoutPreviewBaseActivity : AppCompatActivity() {
112112
* Loads layout information and previews in a snackbar.
113113
*/
114114
private fun showLayoutInfo(layoutInformation: LayoutInformation, fromUser: Boolean = false) {
115-
infoDialog = LayoutInfoDialog.newInstance(
116-
layoutInformation.title.toString(),
117-
layoutInformation.description.toString()
118-
)
119-
infoDialog?.previewXmlListener = { loadLayoutUrl() }
115+
if (infoDialog == null) {
116+
infoDialog = LayoutInfoDialog.newInstance(
117+
layoutInformation.title.toString(),
118+
layoutInformation.description.toString(),
119+
viewModel.layoutUrl
120+
)
121+
}
120122

121-
Timber.d("Layout info showing: %s", infoDialog?.isVisible)
123+
Timber.d("Layout info is showing: %s", infoDialog?.isVisible)
122124
if (infoDialog?.isVisible == false) {
123125
if (fromUser || viewModel.isFirstTime) {
124126
infoDialog?.let {
@@ -130,18 +132,12 @@ open class LayoutPreviewBaseActivity : AppCompatActivity() {
130132
}
131133
}
132134

133-
/**
134-
* Loads currently running layout from Github into chrome web view.
135-
*/
136-
fun loadLayoutUrl() {
137-
val builder = CustomTabsIntent.Builder()
138-
builder.setShowTitle(false)
139-
.addDefaultShareMenuItem()
140-
val customTabsIntent = builder.build()
141-
customTabsIntent.launchUrl(this, Uri.parse(viewModel.layoutUrl))
135+
override fun onStop() {
136+
super.onStop()
137+
Timber.d("Clearing the layout info dialog.")
138+
infoDialog = null
142139
}
143140

144-
145141
//
146142
// Setup menu item on the action bar.
147143
//

0 commit comments

Comments
 (0)