Provides communication with parent Fragment or Activity.
Since version 1.1.0 library's been using from Android X.
In your project level build.gradle
:
allprojects {
repositories {
// ...
maven { url 'https://jitpack.io' }
}
}
And in your app level build.gradle
:
For java version
dependencies {
implementation 'com.github.mac229.FragmentUtils:fragmentutils:1.2.1'
}
For kotlin version
dependencies {
implementation 'com.github.mac229.FragmentUtils:fragmentutils-kt:1.2.1'
}
public class MainActivity extends AppCompatActivity implements ReplaceFragmentListener {
private ReplaceFragmentListener replaceFragmentListener;
@Override
public void onAttach(Context context) {
super.onAttach(context);
replaceFragmentListener = FragmentUtils.getListener(this, ReplaceFragmentListener.class);
}
dependencies {
implementation 'com.github.mac229.FragmentUtils:fragmentutils-kt:1.2.1'
}
class MainActivity : AppCompatActivity(), ReplaceFragmentListener {
private var replaceFragmentListener: ReplaceFragmentListener? = null
override fun onAttach(context: Context?) {
super.onAttach(context)
replaceFragmentListener = getListener()
}
- Get listener or throw exception when parent doesn't implement listener:
private lateinit var replaceFragmentListener: ReplaceFragmentListener
override fun onAttach(context: Context?) {
super.onAttach(context)
replaceFragmentListener = getListenerOrThrowException()
}
- You also can use generic
findFragmentByTag
:
val fragment = supportFragmentManager.findFragmentByTag(TAG)