title: Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
body: <p>I am trying to get permissions for reading calendar, after getting the permissions I am trying to call another fragment. I am stuck with the following exception.This happens for the first launch of the application only.For the next launch application works fine.</p>
  
  <pre><code>5488-5488/com.mindtree.calenderevents E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.mindtree.calenderevents, PID: 5488
  java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=2, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.mindtree.calenderevents/com.mindtree.calenderevents.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
     at android.app.ActivityThread.deliverResults(ActivityThread.java:4998)
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041)
     at android.app.ActivityThread.access$1600(ActivityThread.java:229)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:148)
     at android.app.ActivityThread.main(ActivityThread.java:7325)
     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)
  Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
     at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1533)
     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1551)
     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:696)
     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:662)
     at com.mindtree.calenderevents.MainActivity.onRequestPermissionsResult(MainActivity.java:59)
     at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7291)
     at android.app.Activity.dispatchActivityResult(Activity.java:7169)
     at android.app.ActivityThread.deliverResults(ActivityThread.java:4994)
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041) 
     at android.app.ActivityThread.access$1600(ActivityThread.java:229) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:7325) 
     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>
  
  <p>This is my code</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
          TopSectionFragment fragment = TopSectionFragment.newInstance();
      getSupportFragmentManager().beginTransaction().replace(R.id.fragment, fragment)
              .commit();
  
  }
  
  @Override
  public void getEvents() {
      Log.i("mainacteventmethod ","mainacteventmethod");
      //TODO: implement method to fetch events from calendar and add as fragment arguments
      getPermissionToReadCalender();
  
  }
  
  @Override
  public void onRequestPermissionsResult(int requestCode,
                                         @NonNull String permissions[], @NonNull int[] grantResults) {
      switch (requestCode) {
          case READ_CALENDER: {
  
              if (grantResults.length &gt; 0
                      &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  // permission was granted, yay! Do the
                  // contacts-related task you need to do.
                  RuntimeData.setsCalendarData(getCalender(null));
                  if (RuntimeData.getsCalendarData() != null) {
                      BottomSectionFragment bottomFragment = new BottomSectionFragment();
                      getSupportFragmentManager().beginTransaction().replace(R.id.fragment2,
                              bottomFragment).commit();
                  }
  
              } else {
                  // permission denied, boo! Disable the
                  // functionality that depends on this permission.
              }
              return;
          }
          // other 'case' lines to check for other
          // permissions this app might request
      }
  }
  
  private void getPermissionToReadCalender() {
      if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
  
          if (shouldShowRequestPermissionRationale(
                  Manifest.permission.READ_CALENDAR)) {
          }
          requestPermissions(new String[]{Manifest.permission.READ_CALENDAR},
                  READ_CALENDER);
  
      }else{
          RuntimeData.setsCalendarData(getCalender(null));
          if (RuntimeData.getsCalendarData() != null) {
              BottomSectionFragment bottomFragment = new BottomSectionFragment();
              getSupportFragmentManager().beginTransaction().replace(R.id.fragment2,
                      bottomFragment).commit();
          }
      }
  
  }
  </code></pre>