title: Android : App closes after requesting permissions
body: <p>i have an strange behavior in my Android app. When i request a permission the first time, like Manifest.permission.GET_ACCOUNTS, my app closes (no crash) and the permission dialog appears. </p>
  
  <p>If I decline the permission, start the app again, click the button in a Fragment (who needs the permission to proceed), than the rational dialog is shown the first time and than the permission dialog. But my app gets closed again. If i accept the permission, the app works like expected.</p>
  
  <p>This ugly behavior demolishs the users workflow.</p>
  
  <p>I'm using the <a href="https://github.com/hotchemi/PermissionsDispatcher" rel="nofollow noreferrer">PermissionsDispatcher</a></p>
  
  <p>I had also implemented the request permission like recommanded by Google by my self, but had the same problem, so i switched to the annotation-based PermissionsDispatcher API.</p>
  
  <p>I test my app with the Samsung Galaxy Note Edge and Android 6.0.1</p>
  
  <p>Let me know if I can provide you with more information.</p>
  
  <pre><code>@RuntimePermissions
  public class FragmentTest extends BasicFragment {
  
      @OnShowRationale(Manifest.permission.GET_ACCOUNTS)
      void showRationaleForAll(PermissionRequest request) {
          new AlertDialog.Builder(getContext())
                  .setMessage("This function needs permissions to read your contacts.")
                  .setPositiveButton("YES", (dialog, button) -&gt; request.proceed())
                  .setNegativeButton("NO", (dialog, button) -&gt; request.cancel())
                  .show();
      }
  
      @NeedsPermission(Manifest.permission.GET_ACCOUNTS)
      public void readContacts(){
          // read contacts
      }
  
      @OnPermissionDenied(Manifest.permission.GET_ACCOUNTS)
      void showDeniedForAccounts() {
          Toast.makeText(getContext(), "disabled", Toast.LENGTH_LONG).show();
      }
  
      @Override
      public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
          super.onRequestPermissionsResult(requestCode, permissions, grantResults);
          FragmentTestPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          // ...
  
          view.findViewById(R.id.imageButtonTest).setOnClickListener(clickListenerOnClickView -&gt; {
              testRequestPermission();
          });
  
          // ...
      }
  
      private void testRequestPermission(){
  
          FragmentTestPermissionsDispatcher.readContactsWithCheck(this);
  
          // ...
  
          Dialog dialog = ...
  
          dialog.show(); // &lt;-- **this is the cause of the Exception in the log**
      }
  }
  </code></pre>
  
  <p><strong>UPDATE - LOG without filter</strong></p>
  
  <p>The app log is empty after the click, this is the log without filters. The Exception at the end of the log is thrown by a Dialog.show(). If i remove the Dialog, i have the same problem, the app gets closed and the permission dialog prompts.</p>
  
  <pre><code>11-05 18:16:55.700 5239-5239/de.example.android.test D/FragmentTest: onClick - request permission
  11-05 18:16:55.700 1304-3052/? D/GameManagerService: identifyGamePackage. com.android.packageinstaller
  11-05 18:16:55.700 1304-3052/? D/MultiWindowPolicy: Gamemode - return 0,  when calling  identifyForegroundApp(com.android.packageinstaller)
  11-05 18:16:55.700 1304-3052/? D/CustomFrequencyManagerService: acquireDVFSLockLocked : type : DVFS_MIN_LIMIT  frequency : 1958400  uid : 1000  pid : 1304  pkgName : ACTIVITY_RESUME_BOOSTER@14
  11-05 18:16:55.710 1304-3052/? D/ActivityManager: mDVFSHelper.acquire()
  11-05 18:16:55.710 1304-3052/? V/WindowManager: addAppToken: AppWindowToken{1a4f27 token=Token{2b0ade6 ActivityRecord{b046441 u0 com.android.packageinstaller/.permission.ui.GrantPermissionsActivity t944}}} to stack=1 task=944 at 1
  11-05 18:16:55.720 1304-3052/? D/InputDispatcher: Focused application set to: xxxx
  11-05 18:16:55.720 1304-3052/? D/InputDispatcher: Focus left window: 5239
  11-05 18:16:55.720 1304-1606/? D/PointerIcon: setMouseIconStyle1 pointerType: 1001 iconType:101 flag:0 pid:1304 uid:1000
  11-05 18:16:55.720 1304-1606/? D/PointerIcon: setMouseCustomIcon IconType is same.101
  11-05 18:16:55.720 1304-1606/? D/PointerIcon: setHoveringSpenIconStyle1 pointerType: 10001 iconType:1 flag:0 pid:1304 uid:1000
  11-05 18:16:55.720 1304-1606/? D/PointerIcon: setHoveringSpenCustomIcon IconType is same.1
  11-05 18:16:55.760 2895-2895/? I/CocktailBarVisibilityController: actCloseCocktail by Outside touch
  11-05 18:16:55.760 2895-2895/? D/CocktailBarImmersivePolicy: updateCocktailBarVisibility  onClose
  11-05 18:16:55.760 1304-2354/? I/AbsCocktailBarStatePolicy: handleMessage: entry what = 1
  11-05 18:16:55.760 5239-5239/de.example.android.test I/DialogEditTextWithSuggestedList: -&gt; DialogGuestAccountInfo()
  11-05 18:16:55.760 5239-5239/de.example.android.test D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{bac10c V.ED..... R.....I. 0,0-0,0}
  11-05 18:16:55.760 1304-3484/? D/ISSUE_DEBUG: InputChannelName : 6f2037d de.example.android.test/de.example.android.test.activities.init.ActivityCreateUser
  11-05 18:16:55.770 441-441/? I/SurfaceFlinger: id=676 createSurf (257x257),1 flag=4, BctivityCre
  11-05 18:16:55.770 1304-3052/? I/CocktailPhoneWindowManager: mTmpCocktailBar frame: Rect(1532, 0 - 1692, 2560)
  11-05 18:16:55.780 1304-3052/? I/CocktailPhoneWindowManager: mTmpCocktailBar frame: Rect(1532, 0 - 1692, 2560)
  11-05 18:16:55.790 5239-5239/de.example.android.test D/ActivityCreateUser: **********************************************
  11-05 18:16:55.790 5239-5239/de.example.android.test D/ActivityCreateUser: onPause
  11-05 18:16:55.800 1304-3054/? D/ActivityManager:  Launching com.android.packageinstaller, updated priority
  11-05 18:16:55.810 1304-1304/? D/GameManagerService: NotifyRunnable. pkg: com.android.packageinstaller, type: 4, isMinimized: false, isTunableApp: false
  11-05 18:16:55.810 1304-1304/? D/GameManagerService: unexpected mPrevNotiType: -1
  11-05 18:16:55.810 1304-1453/? V/MARsPolicyManager: updatePackagesScore PackageInfo name -- com.android.packageinstaller
  11-05 18:16:55.810 2895-7024/? D/EdgeTaskManager: onTaskStarted : 1
  11-05 18:16:55.810 2895-7024/? D/EdgeTaskManager: addItem
  11-05 18:16:55.820 5239-5239/de.example.android.test W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
  11-05 18:16:55.820 2895-2895/? D/EdgeTaskManager: onUpdateScroll : 4
  11-05 18:16:55.820 2895-2895/? D/EdgeTaskManager: add : 944, {com.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}
  11-05 18:16:55.820 2895-2895/? D/EdgeTaskManager: updateLayout1 : 5
  11-05 18:16:55.820 5239-5239/de.example.android.test W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
  11-05 18:16:55.830 1304-3050/? I/CocktailPhoneWindowManager: mTmpCocktailBar frame: Rect(1532, 0 - 1692, 2560)
  11-05 18:16:55.870 1304-3483/? V/WindowStateAnimator: Finishing drawing window Window{6f2037d u0 d0 de.example.android.test/de.example.android.test.activities.init.ActivityCreateUser}: mDrawState=DRAW_PENDING
  11-05 18:16:55.870 5239-5239/de.example.android.test D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
  11-05 18:16:55.870 1304-1606/? I/CocktailPhoneWindowManager: mTmpCocktailBar frame: Rect(1532, 0 - 1692, 2560)
  11-05 18:16:55.870 1304-1606/? I/CocktailPhoneWindowManager: mTmpCocktailBar frame: Rect(1532, 0 - 1692, 2560)
  11-05 18:16:55.880 441-441/? D/libEGL: eglInitialize EGLDisplay = 0xbee4d35c
  11-05 18:16:55.880 1304-1606/? D/KnoxTimeoutHandler: notifyActivityDrawn [MsgParam] userId: 0 fullscreen is true showWhenlocked is false
  11-05 18:16:55.880 1304-1304/? D/KnoxTimeoutHandler: activityDrawn [MsgParam] userId: 0 fullscreen is true showWhenlocked is false
  11-05 18:16:55.890 5239-5239/de.example.android.test W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
  11-05 18:16:55.890 1304-1304/? I/KnoxTimeoutHandler: SD activityfalse
  11-05 18:16:55.890 1304-2296/? V/WindowStateAnimator: Finishing drawing window Window{6f2037d u0 d0 de.example.android.test/de.example.android.test.activities.init.ActivityCreateUser}: mDrawState=HAS_DRAWN
  11-05 18:16:55.890 441-441/? D/libEGL: eglInitialize EGLDisplay = 0xbee4d35c
  11-05 18:16:55.910 6531-6531/? D/SecWifiDisplayUtil: Metadata value : none
  11-05 18:16:55.910 6531-6531/? D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{811e448 I.E...... R.....I. 0,0-0,0}
  11-05 18:16:55.910 1304-2296/? D/ISSUE_DEBUG: InputChannelName : eb96b40 com.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity
  11-05 18:16:55.910 1304-3484/? D/ActivityManager: post active user change for 0 fullscreen false isFloatingActivity() false isHomeActivity() false
  11-05 18:16:55.910 1304-3484/? D/KnoxTimeoutHandler: postActiveUserChange [MsgParam] userId: 0 fullscreen is false showWhenlocked is false
  11-05 18:16:55.910 1304-1304/? D/KnoxTimeoutHandler: handleActiveUserChange [MsgParam] userId: 0 fullscreen is false showWhenlocked is false
  11-05 18:16:55.910 1304-1304/? I/KnoxTimeoutHandler: Shared devices show user statefalse
  11-05 18:16:55.910 1304-1304/? I/KnoxTimeoutHandler: Target Activity is not fullscreen. We will not show this activity0
  11-05 18:16:55.920 441-441/? I/SurfaceFlinger: id=677 createSurf (193x193),1 flag=4, HrantPermis
  11-05 18:16:55.920 1304-1457/? D/StatusBarManagerService: manageDisableList userId=0 what=0x0 pkg=Window{eb96b40 u0 d0 com.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}
  11-05 18:16:55.920 1304-3054/? I/CocktailPhoneWindowManager: mTmpCocktailBar frame: Rect(1532, 0 - 1692, 2560)
  11-05 18:16:55.920 2443-2443/? D/PhoneStatusBar: setSystemUiVisibility vis=8000 mask=ffffffff oldVal=8600 newVal=8000 diff=600
  11-05 18:16:55.930 1304-3054/? D/InputDispatcher: Focus entered window: 6531
  11-05 18:16:55.930 1304-3054/? I/CocktailPhoneWindowManager: mTmpCocktailBar frame: Rect(1532, 0 - 1692, 2560)
  11-05 18:16:55.930 1304-1606/? D/PointerIcon: setMouseIconStyle1 pointerType: 1001 iconType:101 flag:0 pid:1304 uid:1000
  11-05 18:16:55.930 1304-1606/? D/PointerIcon: setMouseCustomIcon IconType is same.101
  11-05 18:16:55.930 1304-1606/? D/PointerIcon: setHoveringSpenIconStyle1 pointerType: 10001 iconType:1 flag:0 pid:1304 uid:1000
  11-05 18:16:55.930 1304-1606/? D/PointerIcon: setHoveringSpenCustomIcon IconType is same.1
  11-05 18:16:55.940 1304-6229/? D/ConnectivityService: returning getNetworkInfo for network type 1 : [type: WIFI[] - WIFI, state: CONNECTED/CONNECTED, reason: (unspecified), extra: "HITRON-1DC0", roaming: false, failover: false, isAvailable: true]
  11-05 18:16:55.950 1304-6229/? D/ConnectivityService: returning getNetworkInfo for network type 1 : [type: WIFI[] - WIFI, state: CONNECTED/CONNECTED, reason: (unspecified), extra: "HITRON-1DC0", roaming: false, failover: false, isAvailable: true]
  11-05 18:16:55.960 6531-6531/? D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
  11-05 18:16:55.960 1304-3049/? D/InputMethodManagerService: windowGainedFocus mCurrentFocusedUserId - 0 and mSecureKeypadEnabled-false
  11-05 18:16:55.960 1304-3051/? I/CocktailPhoneWindowManager: mTmpCocktailBar frame: Rect(1532, 0 - 1692, 2560)
  11-05 18:16:55.970 6531-6531/? W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
  11-05 18:16:55.980 1304-2296/? V/WindowStateAnimator: Finishing drawing window Window{eb96b40 u0 d0 com.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}: mDrawState=DRAW_PENDING
  11-05 18:16:55.980 1304-1606/? I/CocktailPhoneWindowManager: mTmpCocktailBar frame: Rect(1532, 0 - 1692, 2560)
  11-05 18:16:55.980 6531-6531/? I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@1cceeb time:5148283
  11-05 18:16:55.990 1304-1606/? I/CocktailPhoneWindowManager: mTmpCocktailBar frame: Rect(1532, 0 - 1692, 2560)
  11-05 18:16:55.990 1304-1606/? D/KnoxTimeoutHandler: notifyActivityDrawn [MsgParam] userId: 0 fullscreen is false showWhenlocked is false
  11-05 18:16:55.990 1304-1304/? D/KnoxTimeoutHandler: activityDrawn [MsgParam] userId: 0 fullscreen is false showWhenlocked is false
  11-05 18:16:55.990 1304-1606/? I/ActivityManager: Displayed com.android.packageinstaller/.permission.ui.GrantPermissionsActivity: +195ms
  11-05 18:16:55.990 1304-1304/? I/KnoxTimeoutHandler: SD activityfalse
  11-05 18:16:55.990 441-441/? D/libEGL: eglInitialize EGLDisplay = 0xbee4d35c
  11-05 18:16:56.010 441-441/? D/libEGL: eglInitialize EGLDisplay = 0xbee4d35c
  11-05 18:16:56.210 1304-1606/? D/CustomFrequencyManagerService: releaseDVFSLockLocked : Getting Lock type frm List : DVFS_MIN_LIMIT  frequency : 1958400  uid : 1000  pid : 1304  tag : ACTIVITY_RESUME_BOOSTER@14
  11-05 18:16:56.220 1304-1606/? D/ActivityManager: mDVFSHelper.release()
  11-05 18:16:56.220 1304-1606/? I/Timeline: Timeline: Activity_windows_visible id: ActivityRecord{b046441 u0 com.android.packageinstaller/.permission.ui.GrantPermissionsActivity t944} time:5148522
  11-05 18:16:56.230 5239-5239/de.example.android.test I/BasicFragment: -&gt; onDetach()
  11-05 18:16:56.230 1304-1606/? I/CocktailPhoneWindowManager: mTmpCocktailBar frame: Rect(1532, 0 - 1692, 2560)
  11-05 18:16:56.240 441-3314/? D/libEGL: eglTerminate EGLDisplay = 0xabacb64c
  11-05 18:16:56.240 441-3314/? D/libEGL: eglTerminate EGLDisplay = 0xabacb64c
  11-05 18:16:56.250 5239-5394/de.example.android.test D/OpenGLRenderer: endAllActiveAnimators on 0x9e75c280 (RippleDrawable) with handle 0x9d6aa200
  11-05 18:16:56.250 5239-5239/de.example.android.test D/ViewRootImpl: #3 mView = null
  11-05 18:16:56.250 441-548/? I/SurfaceFlinger: id=675 Removed BctivityCre (4/10)
  11-05 18:16:56.250 441-5505/? I/SurfaceFlinger: id=675 Removed BctivityCre (-2/10)
  11-05 18:16:56.250 1304-3054/? I/CocktailPhoneWindowManager: mTmpCocktailBar frame: Rect(1532, 0 - 1692, 2560)
  11-05 18:16:56.250 1304-2327/? D/NetworkPolicy: isUidForegroundLocked: 10366, mScreenOn: true, uidstate: 16, mProxSensorScreenOff: false
  11-05 18:16:56.250 1304-1453/? D/CustomFrequencyManagerService: acquireDVFSLockLocked : type : DVFS_MIN_LIMIT  frequency : 1958400  uid : 1000  pid : 1304  pkgName : ACTIVITY_RESUME_BOOSTER@20
  11-05 18:16:56.260 1304-1453/? I/CocktailPhoneWindowManager: mTmpCocktailBar frame: Rect(1532, 0 - 1692, 2560)
  11-05 18:16:56.260 441-441/? D/libEGL: eglTerminate EGLDisplay = 0xbee4d3bc
  11-05 18:16:56.260 441-441/? W/SurfaceFlinger: couldn't log to binary event log: overflow.
  11-05 18:16:56.270 5239-5239/de.codeflakes.android.camtogether E/WindowManager: android.view.WindowLeaked: Activity de.example.android.test.activities.init.ActivityCreateUser has leaked window com.android.internal.policy.PhoneWindow$DecorView{bac10c V.ED..... R....... 0,0-1532,840} that was originally added here
                                                                                      at android.view.ViewRootImpl.&lt;init&gt;(ViewRootImpl.java:565)
                                                                                      at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:326)
                                                                                      at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
                                                                                      at android.app.Dialog.show(Dialog.java:350)
                                                                                      at de.example.android.test.activities.init.fragments.createuser.FragmentTest.readContacts(FragmentTest.java:157)
                                                                                      at de.example.android.test.activities.init.fragments.createuser.FragmentTest.-de_example_android_test_activities_init_fragments_createuser_FragmentTest_lambda$3(FragmentTest.java:110)
                                                                                      at de.example.android.test.activities.init.fragments.createuser.FragmentTest$-android_view_View_onCreateView_android_view_LayoutInflater_inflater_android_view_ViewGroup_container_android_os_Bundle_savedInstanceState_LambdaImpl0.onClick(FragmentTest.java)
                                                                                      at android.view.View.performClick(View.java:5697)
                                                                                      at android.view.View$PerformClick.run(View.java:22526)
                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:158)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:7225)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
  </code></pre>