Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving the scroll position when returning back #2635

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -2,6 +2,7 @@ package leakcanary.internal.activity.screen

import android.app.ActivityManager
import android.app.AlertDialog
import android.os.Parcelable
import android.view.View
import android.view.ViewGroup
import android.widget.ListView
Expand All @@ -23,6 +24,8 @@ import leakcanary.internal.navigation.onCreateOptionsMenu
import leakcanary.internal.navigation.onScreenExiting

internal class HeapDumpsScreen : Screen() {
private var listViewState: Parcelable? = null

override fun createView(container: ViewGroup) =
container.inflate(R.layout.leak_canary_heap_dumps_screen).apply {

Expand Down Expand Up @@ -111,5 +114,10 @@ internal class HeapDumpsScreen : Screen() {
)
countView.text = count
}

if (listViewState != null) {
listView.onRestoreInstanceState(listViewState)
}
onScreenExiting { listViewState = listView.onSaveInstanceState() }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, but this is a bit surprising: there's already a backstack mechanism built in to save and restore view state. When we create a new BackstackFrame instance, the BackstackFrame constructor invokes view.saveHierarchyState(). When we go back and bring a view back to be current, NavigatingActivity.goBack() latest.restore(currentView) which calls view.restoreHierarchyState(viewState)

Before adding this new behavior, I'd want to understand why the normal view state saving behavior isn't working here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I debugged through this, and the explanation here is that the view state is getting restored when we navigate back, but at that point the listview has no adapter. We load the data from the db async and restore the view state afterwards.

}
}
@@ -1,5 +1,6 @@
package leakcanary.internal.activity.screen

import android.os.Parcelable
import android.text.Html
import android.text.SpannableStringBuilder
import android.util.Patterns
Expand Down Expand Up @@ -28,6 +29,7 @@ import leakcanary.internal.navigation.Screen
import leakcanary.internal.navigation.activity
import leakcanary.internal.navigation.goTo
import leakcanary.internal.navigation.inflate
import leakcanary.internal.navigation.onScreenExiting
import shark.HeapAnalysisSuccess
import shark.LeakTrace
import shark.LibraryLeak
Expand All @@ -37,6 +39,8 @@ internal class LeakScreen(
private val leakSignature: String,
private val selectedHeapAnalysisId: Long? = null
) : Screen() {
private var listViewState: Parcelable? = null

override fun createView(container: ViewGroup) =
container.inflate(R.layout.leak_canary_leak_screen)
.apply {
Expand Down Expand Up @@ -248,6 +252,11 @@ internal class LeakScreen(

val adapter = DisplayLeakAdapter(context, leakTrace, title)
listView.adapter = adapter

if (listViewState != null) {
listView.onRestoreInstanceState(listViewState)
}
onScreenExiting { listViewState = listView.onSaveInstanceState() }
}

private fun leakToString(
Expand Down