2

我正在尝试制作一个AlertDialog在 aFragment可见时显示的setUserVisibleHint(boolean),它告诉用户打开他们的 GPS 设置。我正在使用这段代码:

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,然后转到屏幕,它工作正常。谁能建议为什么?

编辑:在@pax2K 的建议下,我包含了一段 logcat。我在上面的代码中添加了日志语句:

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)

两边都是来自代码完全不同部分的调试语句。我并不聪明。

4

1 回答 1

1

setUserVisibleHint(boolean)实际上不是生命周期方法:它只是告诉您用户可以看到您的Fragment. 我原以为它直到之后才会被击中onResume();我错了。(顺便说一句,我一开始没有使用的原因是我们在它们不可见onResume()的情况下进行了预加载。)Fragments

放入代码以确保两者 都已被调用已解决此问题onResume() setUserVisibleHint(boolean)

于 2013-03-19T17:22:55.287 回答