在 Tab Widget 中使用 onPause、onRestart - Android
我有一个位于 TabWidget
内的 ListView
。当我在 ListView
上选择一个项目并转到子 ListView
时,TabWidget
消失。这很好,只是它会调用 onPause
方法,从而导致当我返回父 ListView
时调用 onRestart
。
我有 onRestart
设置来从服务器检索更新的数据,但我不希望每次用户返回到父 ListView
时都会发生这种情况。我只希望当应用程序从后台运行中恢复时调用 onRestart
。我尝试实现一个布尔变量来确定是否应该执行 onRestart
中的代码,但似乎没有办法避免这种影响。
有想法吗?
I have a ListView
that is inside a TabWidget
. When I select an item on the ListView
and go to the child ListView
, the TabWidget
disappears. This is fine, except that it invokes the onPause
method, and thus causes onRestart
to be called when I return to the parent ListView
.
I have onRestart
setup to retrieve updated data from the server, but I do not want this to occur everytime the user returns to the parent ListView
. I only want onRestart
to be called when the app comes alive from running in the background. I have tried implementing a Boolean variable to determine if I should execute the code that is inside onRestart
, but there doesn't seem to be a way to get around the effects of this.
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
startActivityForResult
触发您的子活动,并在onActivityResult
中设置一个标志以不重新加载(当用户从详细信息页面返回到列表视图时应该调用该标志)。您将有一个成员变量(假设为mReload
),并在onCreate
和onRestart
中将其设置为true
>,并设置为onActivityResult
中的false
,然后在mReload
为 true 时处理重新加载onResume
(并将其设置回来)为false
因此正常的恢复不会触发重新加载)。替代方案:只需在您的活动中该行的点击侦听器上设置一个标志 (
mLeftPage
)。重新启动时,如果mLeftPage
为 true,则不重新加载列表并将mLeftPage
设置回 false。否则,重新加载。You could fire off your child activity with
startActivityForResult
and set a flag inonActivityResult
to not reload (which should be called when the user backs into the listview from the detail page). You'd have a member variable (let's saymReload
) that you'd set totrue
inonCreate
andonRestart
, and tofalse
inonActivityResult
, then process the reloadonResume
ifmReload
is true (and set it back tofalse
so a normal resume won't trigger the reload).Alternative: just set a flag (
mLeftPage
) in your activity on the row's click listener. On restart, ifmLeftPage
is true, don't reload the list and setmLeftPage
back to false. Otherwise, reload.