Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Androidx Activity OnBackPressedCallback Interface Issue

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 {
 //...
}
like image 286
display name Avatar asked Mar 14 '19 23:03

display name


People also ask

What is Androidx activity?

Provides the base Activity subclass and the relevant hooks to build a composable structure on top. Over 291 Thousand. Total number of downloads. Over 755 Billion.

What is ComponentActivity Android?

ComponentActivity is a base class for activities that enable the composition of higher-level components. It extends ComponentActivity from androidx.core.app class ComponentActivity extends Activity.


1 Answers

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

like image 129
ianhanniballake Avatar answered Sep 27 '22 21:09

ianhanniballake