问题描述
我试图使一个 AlertDialog
,其中显示,当片段
可见使用 setUserVisibleHint(布尔)
,告诉用户他们的GPS设置切换。我用这code:
I'm trying to make an AlertDialog
that displays when a Fragment
is visible using setUserVisibleHint(boolean)
, that tells the user to switch on their GPS settings. I am using this code:
new AlertDialog.Builder(getActivity())
.setTitle(R.string.dialogLocationDisabledTitle)
.setMessage(R.string.dialogLocationDisabledMsg)
.setCancelable(true)
.setPositiveButton(R.string.gpssettings, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
mRecheckLocationOnResume = true;
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create()
.show();
不过,当你进入禁用GPS应用程序,并转到屏幕,无论是按钮显示。当你进入应用程序,关掉GPS,然后去到屏幕上,它工作正常。任何人都可以说明为什么?
However, when you enter the app with GPS disabled, and go to the screen, neither button displays. When you enter the app, switch off GPS, then go to the screen, it works fine. Can anyone suggest why?
编辑:在@ pax2K的建议下,我包括logcat的片段。我添加记录语句到上述code:
at the suggestion of @pax2K, I include a snippet of logcat. I added logging statements into the above code:
03-19 16:05:40.062: D/LocateServiceFragment(11193): Starting AlertDialog build
03-19 16:05:40.062: D/LocateServiceFragment(11193): Adding positive button
03-19 16:05:40.072: D/LocateServiceFragment(11193): Adding negative button
03-19 16:05:40.072: D/LocateServiceFragment(11193): Building AlertDialog
03-19 16:05:40.072: D/LocateServiceFragment(11193): Showing AlertDialog
03-19 16:05:40.182: D/LocateServiceFragment(11193): AlertDialog shown
03-19 16:05:40.803: I/Adreno200-EGLSUB(11193): <ConfigWindowMatch:2089>: Format RGBX_8888.
03-19 16:05:40.903: I/Adreno200-EGLSUB(11193): <ConfigWindowMatch:2078>: Format RGBA_8888.
03-19 16:05:41.123: D/dalvikvm(11193): GC_FOR_ALLOC freed 1560K, 19% free 17223K/21219K, paused 48ms
03-19 16:05:41.363: D/OpenGLRenderer(11193): has fontRender patch
03-19 16:05:42.084: D/memalloc(11193): ashmem: Mapped buffer base:0x5576b000 size:737280 fd:166
03-19 16:05:45.668: D/memalloc(11193): /dev/pmem: Unmapping buffer base:0x56ff8000 size:16613376 offset:15998976
03-19 16:05:45.668: D/memalloc(11193): /dev/pmem: Unmapping buffer base:0x53882000 size:614400 offset:0
03-19 16:05:45.668: D/memalloc(11193): /dev/pmem: Unmapping buffer base:0x57fd0000 size:1966080 offset:1351680
03-19 16:05:45.958: D/memalloc(11193): /dev/pmem: Unmapping buffer base:0x5857d000 size:4239360 offset:3502080
03-19 16:05:45.958: D/memalloc(11193): /dev/pmem: Unmapping buffer base:0x55e8a000 size:1351680 offset:614400
03-19 16:05:46.178: D/OpenGLRenderer(11193): Flushing caches (mode 1)
一左一右是从code完全不同的部分调试语句。我没有太多的收获。
Either side are debug statements from completely different parts of the code. I am not much the wiser.
推荐答案
setUserVisibleHint(布尔)
并不是一个真正的生命周期方法:它只是告诉你用户可以看到您的片段
。我曾以为它不会被打,直到后 onResume()
;我错了。 (顺便说一句,我没有使用的原因 onResume()
中的第一个地方是,我们是pre-装载片段
当他们不可见的。)
setUserVisibleHint(boolean)
is not actually a lifecycle method: it just tells you the user can see your Fragment
. I had assumed it wouldn't be hit till after onResume()
; I was wrong. (Incidentally, the reason I wasn't using onResume()
in the first place was that we are pre-loading Fragments
while they're not visible.)
请辞code确保的两个的 onResume()
的和的 setUserVisibleHint(布尔)
被称为已经修复了这个问题。
Putting code in to make sure both onResume()
and setUserVisibleHint(boolean)
have been called has fixed this issue.
这篇关于AlertDialog.Builder不从setUserVisibleHint(布尔值)在片段中显示的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!