AlertDialog.Builder not displaying buttons from se

2019-07-21 18:19发布

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();

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?

EDIT: 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)

Either side are debug statements from completely different parts of the code. I am not much the wiser.

1条回答
Deceive 欺骗
2楼-- · 2019-07-21 18:59

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.)

Putting code in to make sure both onResume() and setUserVisibleHint(boolean) have been called has fixed this issue.

查看更多
登录 后发表回答