6
implementation("androidx.core:core-ktx:1.1.0-alpha04")
implementation ("androidx.appcompat:appcompat:1.0.2")
implementation ("androidx.activity:activity-ktx:1.0.0-alpha05")

I was also looking at this Why are their 2 different ComponentActivity classes? I have this library setup and hoping to use OnBackPressedCallback

import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment

open class BaseNiceFragment : Fragment(), OnBackPressedCallback {

    override fun handleOnBackPressed(): Boolean {
       // do some nice things here
       return true
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
       super.onActivityCreated(savedInstanceState)
       activity!!.addOnBackPressedCallback(viewLifecycleOwner, this)
    }

    override fun onDestroyView() {
       super.onDestroyView()
       activity!!.removeOnBackPressedCallback(this)
    }

    protected open fun handleEmptyView(isListEmpty: Boolean) {} 
}

It works fine with debug builds with Android Studio but whenever I try to do a app:compileFullReleaseKotlin, I keep running into the following issues

BaseNiceFragment.kt: (_, _): Unresolved reference: OnBackPressedCallback

Anyone or Ian know how to make sure this is included in all of my builds

public interface OnBackPressedCallback {
 //...
}
display name
  • 879
  • 11
  • 20

2 Answers2

17

You're mixing stable releases (appcompat:1.0.2 and the fragment:1.0.0 it depends on) with alpha releases. Only Fragment 1.1.0 alpha versions of FragmentActivity depend on androidx.activity's ComponentActivity and therefore include the addOnBackPressedCallback method.

You need to specifically include androidx.fragment:fragment:1.1.0-alpha05 or switch your AppCompat dependency to androidx.appcompat:appcompat:1.1.0-alpha03 (which transitively depends on Fragment 1.1.0-alpha05).

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • If this answered your question, make sure to accept the answer to remove the question from the queue of unanswered questions – ianhanniballake Mar 15 '19 at 15:22
  • @ianhanniballake `addOnBackPressedCallback` is gone in 1.1.0-alpha08. What should I use ? – vovahost May 17 '19 at 09:47
  • 1
    @vovahost - as per [the documentation](https://developer.android.com/guide/navigation/navigation-custom-back), you should use `getOnBackPressedDispatcher()` and the methods available on that class. – ianhanniballake May 17 '19 at 13:59
  • 1
    This is so confusing... I just want to try out some of the fancy new stuff you showed at Google IO and end up trying out dependencies the whole day. In my case it was `lifecycleScope` which somehow needed `Cancellable` which somehow lead to this error after I've put in some random dependencies out of the hundreds available in the ktx cyberspace. The chosen ones can even have a `-ktx` suffix which may contain just the functionality you want - just try it out. – mbo May 19 '19 at 20:30
4

As of August 2019 this is all I needed instead of loading other dependencies as the top answer androidx.appcompat:appcompat:1.1.0-rc01

Juan Mendez
  • 2,658
  • 1
  • 27
  • 23