fragment navigation extension function does not work - android

I have created this extension function for navigating between fragments which is pretty straight forward but somehow it's not working. it's not doing anything and nothing changes when I click the button. I think the problem is with this#navigate argument but I don't see why that should be a problem.
fun Fragment.navigate(): Int? {
return fragmentManager?.run {
beginTransaction()
.replace(
R.id.my_container,
this#navigate,
this#navigate::class.simpleName
)
.commit()
}
}
and the usage is like this
class TestTwoFragment : Fragment(R.layout.fragment_test_two) {
....
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
...
goto_three.setOnClickListener {
TestThreeFragment
.newInstance()
.navigate()
}
}

You're using the fragmentManager of the new fragment which will be null if the fragment hasn't been added yet, like in your case. Since you're using ?.run, nothing happens and the method returns null.
Consider adding a fragment manager parameter to your method:
fun Fragment.navigate(fm: FragmentManager): Int? {
return fm.run {
beginTransaction()
.replace(
R.id.my_container,
this#navigate,
this#navigate::class.simpleName
)
.commit()
}
}
And then:
TestThreeFragment
.newInstance()
.navigate(getParentFragmentManager())

Related

Fragment doesn't register in backstack

I have the following method
// Change fragment extension function to handle navigation easily
fun changeFragment(fragmentManager: FragmentManager?, #IdRes containerId: Int, fragment: Fragment?, addToBackStack: Boolean = false) {
if (fragmentManager == null || fragment == null) return
val fragmentTransaction = fragmentManager.beginTransaction()
if (addToBackStack) fragmentTransaction.addToBackStack(null)
fragmentTransaction.replace(containerId, fragment, fragment::class.java.simpleName).commit()
}
and I am using it inside one of my fragments -
class InspectionFragment : Fragment(R.layout.fragment_inspection) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
inspectionAdapter = InspectionAdapter { inspection ->
changeFragment(
parentFragmentManager, R.id.fragment_inspection_frame_layout,
InspectionDetailsFragment.newInstance(inspection), true
)
}
}
}
class InspectionDetailsFragment : Fragment() {
companion object {
fun newInstance(inspection: Inspection): InspectionDetailsFragment {
val inspectionDetailsFragment = InspectionDetailsFragment()
val bundle = Bundle()
bundle.putParcelable(GeneralConstants.INSPECTION, inspection)
inspectionDetailsFragment.arguments = bundle
return inspectionDetailsFragment
}
}
}
The thing is that, when I instansiate onetop of the 'InspectionDetailsFragment' layer another fragment and press back, it goes directly back to the 'InspectionFragment' parent.
I can't understand why this is happening.
Anyone has an idea?
When you call changeFragment function, you're passing true as last argument:
changeFragment(
parentFragmentManager, R.id.fragment_inspection_frame_layout,
InspectionDetailsFragment.newInstance(inspection), true
)
In changeFragment function:
if (addToBackStack) fragmentTransaction.addToBackStack(null)
It doesn't get added to backStack. Instead of null i think you should pass String value as the name of the backStack you want to use.
I'm not 100% sure about this. You can read more about fragment transactions from here: https://developer.android.com/guide/fragments/transactions

Button lose listener after fragment replace

I have the weirdest bug on Kotlin, and after two days of trying I finally asking for help.
The problem is simple : I have two fragment and one activity, the first fragment A is a form, with a validate button, when I click on validate, the fragment B replace the fragment A, and if I press back, the fragment A show up again with the form filled.
My problem is that after the fragment is shown again, I can click on the button but the listener is not call, so I can't go to the fragment B again. The strange thing is that the other listener are properly working, so I'm thinking it's because the previous fragment is catching the onClick, but idk what to do. Here is some code :
ViewUtils :
fun addFragment(activity: Activity, fragment: androidx.fragment.app.Fragment, container: Int) {
val fragmentManager = (activity as AppCompatActivity).supportFragmentManager
val pendingTransaction = fragmentManager.beginTransaction().add(container, fragment, fragment.javaClass.name)
pendingTransaction.commitAllowingStateLoss()
}
fun replaceFragment(manager: FragmentManager, fragment: androidx.fragment.app.Fragment, container: Int) {
if (fragment.isAdded) return
val pendingTransaction = mangaer.beginTransaction()
pendingTransaction.replace(container, fragment, fragment.javaClass.name)
pendingTransaction.commitAllowingStateLoss()
}
fun removeFragment(activity: Activity, fragment: Fragment) {
val manager = (activity as AppCompatActivity).supportFragmentManager
val trans = manager.beginTransaction()
trans.remove(fragment)
trans.commit()
manager.popBackStack()
}
Activity :
fun displayFragmentA() {
ViewUtils.replaceFragment(supportFragmentManager, FragmentA,
R.id.fragmentLayout)
}
fun FragmentB() {
ViewUtils.replaceFragment(supportFragmentManager, FragmentB,
R.id.fragmentLayout)
}
Fragment A
class AFragment : BaseFragment(), AContract.View {
companion object {
#JvmStatic
fun newInstance(): AFragment {
val fragment = AFragment()
return fragment
}
}
#Inject
lateinit var APresenter: AContract.Presenter<AContract.View>
//end region
//region lifecycle
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_A_layout, container, false)
}
override fun onResume() {
super.onResume()
button_validate.setOnClickListener {
presenter.goToNextStep()
}
}
override fun onAttach(context: Context) {
AndroidSupportInjection.inject(this)
super.onAttach(context)
}
The listener was set in the onViewCreated but I tried moving it to onResume (didn't change anything)
Fragment B code is not important I think, but I can add it if it helps.
Any help is welcome, I really don't know what's going on, the replace/add methods were there before I came to the project, they are not perfect but they are working elsewhere on the project.
I try using breakpoint, the button is not null but we never enter the listener.
Edit : I tried on 3 differents devices, I don't have the bug with a Sony Android 9, but with Huawei et One plus 6 Android 10, the problem persist ..
Ok so after asking to a lot of people, the only solution I found is not using kotlin.synthetic, and using findById instead :
view.findViewById<Button>(R.id.button_validate.setOnClickListener

Using androix backButtonPressedCallback

I need to implement a custom onBackButtonPress method for Fragments. example in LogoutFragment, after logout is handled. user cannot go backStack but a message is shown like press again to exist and is exited. I used this solution. But is not working. Then I saw this Android Doc with onBackPressedDispatcher callback method CODE BELOW. I guess this will work. I added dependencies, but how to implement this in a different fragments, with only 1 activity and fragment container. Kotlin version.
class LogoutFragment : DaggerFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
...
}
...
}
...
}
onBackPressedDispatcher gives an opportunity to handle back press differently in every fragment. Hence you may have to give this piece of code in every fragment.
In the fragment:
var doubleBackToExitPressedOnce = false
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
requireActivity().onBackPressedDispatcher.addCallback(this, callback)
}
val callback = object : OnBackPressedCallback(true ) {
override fun handleOnBackPressed() {
if (doubleBackToExitPressedOnce)
requireActivity().finish()
Toast.makeText(requireContext(), "Press again to go back",
Toast.LENGTH_LONG).show()
doubleBackToExitPressedOnce = true
}
}
If you want to handle back presses of all fragments in one place, you can do so by manipulating the doBack() method in your reference: How to implement onBackPressed() in Fragments?
//pseudocode
fun doBack()
{
//find the fragment
val fragment = supportFragmentManager.findFragmentByTag(
//your fragment tag
)
if(fragment is FragmentA)
{
//do something
}
else if(fragment is FragmentB)
{
//do something else
}
else
{
activity.getSupportFragmentManager().popBackStack(null,
FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}

Passing Interface To Fragment From Activity

I am trying to send information from fragment to the main activity.
I am trying to set a var interfaceName in a fragment from the main activity.
I created var menuInterface: MenuInterface and tried to set it in onNavigationItemSelected using
myFragment.menuInterface = this
The menuInterface stays null for some reason... any idea why?
the onNavigationItemSelected
override fun onNavigationItemSelected(menuItem: MenuItem): Boolean {
when (menuItem.itemId) {
R.id.feedLayoutId -> {
feedFragment = FeedFragment()
feedFragment.menuInterface = this
barTitle.text = "myTitle"
supportFragmentManager
.beginTransaction()
.replace(R.id.frame_layout, feedFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
}
drawerLayout.closeDrawer(GravityCompat.START)
return true
}
Implement MenuInterface in your activity,and remove this line from ur code.
feedFragment.menuInterface = this
In ur fragment:
private var menuInterface: MenuInterface? = null
override fun onAttach(context: Context?) {
super.onAttach(context)
menuInterface = context as MenuInterface
}
override fun onDetach() {
menuInterface = null
super.onDetach()
}
I have already provided solution for such question. https://stackoverflow.com/a/35038574/3027124
The idea is communication between activity and fragment should be done through interfaces or you should implement MVVM architecture and use the same view model for both fragment and activity.
Useful resources:
https://developer.android.com/training/basics/fragments/communicating
MVVM Overview https://developer.android.com/jetpack/docs/guide#overview

How to change Fragment Kotlin

I'm starting in Kotling and I don't know how to change between fragments, I have tried this code:
val manager = supportFragmentManager
val transaction = manager.beginTransaction()
transaction.add(R.layout.fragment_information.toInt(), ComplainFragment())
transaction.commit()
R.layout.fragment_information.toInt()
But i have an error with this parameter because it doesn't find the fragment Id.
I usually use replace to change between fragments. Also change R.layout.fragment_information to R.id.fragment_layout_id only, so no need toInt()
transaction.replace(R.id.fragment_layout_id, fragment)
Here is my suggestion.
var fragment: Fragment? = null
when (itemId) {
R.id.fragment_information -> {
fragment = ComplainFragment()
}
}
if (fragment != null) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_layout_id, fragment)
transaction.commit()
}
The other answers will work but still we can improve a lot by using extension functions in Kotlin.
Add an extension function to the FragmentManager class like below,
inline fun FragmentManager.doTransaction(func: FragmentTransaction.() ->
FragmentTransaction) {
beginTransaction().func().commit()
}
then create an extension function to the AppCompatActivity class,
fun AppCompatActivity.addFragment(frameId: Int, fragment: Fragment){
supportFragmentManager.doTransaction { add(frameId, fragment) }
}
fun AppCompatActivity.replaceFragment(frameId: Int, fragment: Fragment) {
supportFragmentManager.doTransaction{replace(frameId, fragment)}
}
fun AppCompatActivity.removeFragment(fragment: Fragment) {
supportFragmentManager.doTransaction{remove(fragment)}
}
Now, to add and remove fragments from any activity, you just need to call like this,
addFragment(R.id.fragment_container, fragment)
replaceFragment(R.id.fragment_container, fragment)
please refer the below link for more info,
https://medium.com/thoughts-overflow/how-to-add-a-fragment-in-kotlin-way-73203c5a450b
This is an example for you to go to a fragment or activity by clicking a button inside another fragment:
class Fragment_One: Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btn_goToActivity2.setOnClickListener {
val intent = Intent(context, SecondActivity::class.java)
startActivity(intent)
}
btn_goToFragment2.setOnClickListener {
var fr = getFragmentManager()?.beginTransaction()
fr?.replace(R.id.fragment, Fragment_Two())
fr?.commit()
}
}
}
When you add a fragment, you need to add it to an ID that exists in your Activity's layout, not an entire layout:
supportFragmentManager.beginTransaction().add(R.id.some_id_in_your_activity_layout, ComplainFragment()).commit()
In case anyone still needs a quick approach to this. I created a function than can be easily called whenever you need to change a fragment.
private fun replaceFragment(fragment: Fragment) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.frame, fragment)
transaction.commit()
}
R.id.frame in this case is the id of my Framelayout in the activity that will hold my fragment. All you have to do now is call the function.
replaceFragment(HomeFragment())
private fun transitionFragment(fragment: Fragment) {
val transition = requireActivity().supportFragmentManager.beginTransaction()
transition.replace(R.id.fragment_container_create_void_parent, fragment)
.addToBackStack(null).commit()
}
fragment-ktx jetpack library contains convenient extension functions which simplify many things, including transactions:
// MyActivity.kt
class MyActivity : AppCompatActivity() {
...
fun showMyFragment() {
val fragment = MyFragment()
supportFragmentManager.commit {
replace(R.id.fragment_container, fragment)
}
}
}
R.id.fragment_container it's an id of a fragment container in the parent layout. There's FragmentContainerView which is the recommended container, for example:
<!-- my_activity_layout.xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout ...>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragment_container"
... />
...
</androidx.constraintlayout.widget.ConstraintLayout>
But if your purpose is to implement in-app navigation, it's better and much easier to use Navigation component instead of manually switching fragments.
this is my solution for Change current fragment to orther in kotlin:
val supportFragment = SupportFragment()
requireActivity().supportFragmentManager.beginTransaction()
.add(this.id, supportFragment)
.addToBackStack("ok")
.commit()

Categories

Resources