Skip to content

Commit a5d71bc

Browse files
committed
修复崩溃
1 parent 6959a80 commit a5d71bc

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

app/src/main/java/remix/myplayer/misc/Ext.kt

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package remix.myplayer.misc
22

3+
import android.app.Activity
34
import android.app.PendingIntent
45
import android.content.Context
56
import android.content.res.Configuration
@@ -171,3 +172,6 @@ fun DavResource.isAudio(): Boolean {
171172
return musicExt.contains(ext.lowercase(Locale.getDefault())) &&
172173
(contentType == "application/octet-stream" || contentType.startsWith("audio"))
173174
}
175+
176+
// glide加载图片之前检查activity是否被销毁
177+
fun Context.isValidGlideContext() = this !is Activity || (!this.isDestroyed && !this.isFinishing)

app/src/main/java/remix/myplayer/ui/activity/MainActivity.kt

+8-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import remix.myplayer.misc.cache.DiskCache
4343
import remix.myplayer.misc.handler.MsgHandler
4444
import remix.myplayer.misc.handler.OnHandleMessage
4545
import remix.myplayer.misc.interfaces.OnItemClickListener
46+
import remix.myplayer.misc.isValidGlideContext
4647
import remix.myplayer.misc.menu.LibraryListener.Companion.EXTRA_COVER
4748
import remix.myplayer.misc.receiver.ExitReceiver
4849
import remix.myplayer.misc.update.DownloadService
@@ -128,6 +129,7 @@ class MainActivity : MenuActivity(), View.OnClickListener {
128129

129130
override fun onDestroy() {
130131
super.onDestroy()
132+
handler.removeCallbacksAndMessages(null)
131133
unregisterLocalReceiver(receiver)
132134
}
133135

@@ -169,7 +171,7 @@ class MainActivity : MenuActivity(), View.OnClickListener {
169171
SPUtil.putValue(this, SPUtil.OTHER_KEY.NAME, SPUtil.OTHER_KEY.LAST_OPEN_TIME, System.currentTimeMillis())
170172
if (System.currentTimeMillis() - lastOpenTime >= TimeUnit.DAYS.toMillis(2)) {
171173
handler.postDelayed({
172-
Theme.getBaseDialog(this)
174+
showDialog(Theme.getBaseDialog(this)
173175
.title(R.string.support_developer)
174176
.positiveText(R.string.go)
175177
.negativeText(R.string.cancel)
@@ -182,7 +184,7 @@ class MainActivity : MenuActivity(), View.OnClickListener {
182184
.onNeutral { _, _ ->
183185
SPUtil.putValue(this, SPUtil.OTHER_KEY.NAME, SPUtil.OTHER_KEY.SUPPORT_NO_MORE_PROMPT, true)
184186
}
185-
.show()
187+
.build())
186188
}, 1500)
187189
}
188190
}
@@ -639,13 +641,16 @@ class MainActivity : MenuActivity(), View.OnClickListener {
639641
val currentSong = MusicServiceRemote.getCurrentSong()
640642
if (currentSong != Song.EMPTY_SONG) {
641643
binding.navigationView.findViewById<TextView>(R.id.tv_header).text = getString(R.string.play_now, currentSong.title)
642-
Glide.with(this)
644+
if (isValidGlideContext()) {
645+
Glide.with(this)
643646
.load(currentSong)
644647
.centerCrop()
645648
.signature(ObjectKey(UriFetcher.albumVersion))
646649
.placeholder(Theme.resolveDrawable(this, R.attr.default_album))
647650
.error(Theme.resolveDrawable(this, R.attr.default_album))
648651
.into(binding.navigationView.findViewById(R.id.iv_header))
652+
}
653+
649654
}
650655
}
651656

app/src/main/java/remix/myplayer/ui/activity/base/BaseActivity.kt

+12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ open class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope() {
4242

4343
var toDeleteSongs: ArrayList<Song>? = null
4444

45+
private var dialog: Dialog? = null
46+
4547
private val loadingDialog by lazy {
4648
Theme.getBaseDialog(this)
4749
.title(R.string.loading)
@@ -124,6 +126,7 @@ open class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope() {
124126
override fun onDestroy() {
125127
super.onDestroy()
126128
isDestroyed = true
129+
dialog?.dismiss()
127130
}
128131

129132
override fun isDestroyed(): Boolean {
@@ -201,6 +204,15 @@ open class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope() {
201204
loadingDialog.dismiss()
202205
}
203206
}
207+
208+
protected fun showDialog(newDialog: Dialog) {
209+
dialog?.dismiss()
210+
if (!isFinishing && !isDestroyed && hasWindowFocus()) {
211+
dialog = newDialog
212+
newDialog.show()
213+
}
214+
}
215+
204216

205217
companion object {
206218
val NECESSARY_PERMISSIONS =

app/src/main/java/remix/myplayer/ui/misc/MultipleChoice.kt

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.app.Activity
55
import android.text.TextUtils
66
import android.view.View
77
import android.widget.PopupMenu
8+
import com.tencent.bugly.crashreport.CrashReport
89
import io.reactivex.Single
910
import io.reactivex.android.schedulers.AndroidSchedulers
1011
import io.reactivex.disposables.CompositeDisposable
@@ -413,7 +414,11 @@ class MultipleChoice<T>(activity: Activity, val type: Int) {
413414
true
414415
}
415416
if (!activity.isFinishing && !activity.isDestroyed && activity.hasWindowFocus()) {
416-
show()
417+
try {
418+
show()
419+
} catch (e: Exception) {
420+
CrashReport.postCatchedException(Exception("ac: $activity", e))
421+
}
417422
}
418423
}
419424
}

0 commit comments

Comments
 (0)