title: Where to keep common context dependent code between fragment and Activity, except for ViewModel?
body: <p>I am creating this new android application and I am stuck in this thought process. I have an Activity which has a bottom sheet, in which you can choose to pick image from Gallery or Camera to add an attachment to a post. So I need to request permissions, listen for permission callback, parse the gallery/camera results and toggle the UI that's a lot of code. And the thing is I have a fragment which does the exact same thing to upload a profile picture. If it were two activities, I could just add the code to a baseActivity. But its a fragment and an Activity. On the other hand, you should not bloat your ViewModel either and these two components are completely independent. </p>
  
  <p>I have thought of putting it in a ViewModel, but cannot put views in it.
  Also, I somehow know that this requirement is a common one.
  I am aware of the presenter pattern, but still you cannot put views in it.</p>
  
  <pre><code>// starting gallery
  private fun startGallery(type: String, title: String, requestCode: Int){
      val intent = Intent()
      intent.type = type
      intent.action = Intent.ACTION_PICK
      startActivityForResult(Intent.createChooser(intent, title), 
      requestCode)
  }
  
  // starting camera intent
  private fun startCameraFor(action: String, requestCode: Int) {
      val takeVideoIntent = Intent(action)
      if (takeVideoIntent.resolveActivity(context?.packageManager) != null) 
  {
          startActivityForResult(takeVideoIntent, requestCode)
      }
  }
  
   // Handling image results
   override fun onActivityResult(requestCode: Int, resultCode: Int, data: 
   Intent?) {
      super.onActivityResult(requestCode, resultCode, data)
      if(resultCode == Activity.RESULT_OK) {
          postViewModel.uploadImageResults(data = data, requestCode = 
          requestCode, userId = "c7c2352b-5449-43bf-a099-688843025130")
      }
      toggleBottomSheet()
  }
  
    // Check for permission
    private fun permissionAction(permission: String =  
    Manifest.permission.READ_EXTERNAL_STORAGE, callback: () -&gt; Unit){
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN) {
          val checkPermission = ContextCompat.checkSelfPermission(context!!, 
          permission)
          if (checkPermission != PackageManager.PERMISSION_GRANTED) {
              ActivityCompat.requestPermissions(activity!!, 
              arrayOf(permission), Constants.REQUEST_CODE_STORAGE_PICK)
          } else {
              callback()
          }
      }
  }
  
  // Toggles bottom sheet
  private fun toggleBottomSheet() {
      if(bottomSheet.state == BottomSheetBehavior.STATE_COLLAPSED) {
          bottomSheet.state = BottomSheetBehavior.STATE_EXPANDED
      } else {
          bottomSheet.state = BottomSheetBehavior.STATE_COLLAPSED
      }
  }
  </code></pre>