Load a fragment from another fragment on button click

Issue

I am brand new to mobile app development and sort of stumbling my way through this app but I have been battling to try and work this out. I have wanted numerous tutorials, hunted developer sites and searched Stack Overflow but nothing I do seems to work, please could you help me? I am using Android Studio //Dolphin | 2021.3.1 Patch 1 and coding in Kotlin.

All I want to do is open the fragment_sign when I click the button android:id="@+id/button_customer_sign" from the fragment_delivery and then once the customer has signed return back to fragment_delivery when I click the button android:id="@+id/button_load_sign" from the fragment_sign.

I have the following in my build.gradle

dependencies {

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.annotation:annotation:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
    implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.kyanogen.signatureview:signature-view:1.2'

    implementation 'androidx.fragment:fragment-ktx:1.6.0-alpha03'
    implementation 'androidx.fragment:fragment:1.6.0-alpha03'
    debugImplementation 'androidx.fragment:fragment-testing:1.6.0-alpha03'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'

    // google support library ---------------------------------------------------------------------
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
    implementation 'androidx.browser:browser:1.4.0'
}

currently this is what is in my DeliveryFragment.kt

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.example.delivery.R
import com.example.delivery.databinding.FragmentDeliveryBinding
import com.example.delivery.ui.sign.Sign

import android.support.v4.app.Fragment //This import is an error
import android.support.v4.app.FragmentManager //This import is an error
import android.support.v4.app.FragmentTransaction //This import is an error

class DeliveryFragment : Fragment() {

    private var _binding: FragmentDeliveryBinding? = null
    private var customerSign: Button? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val deliveryViewModel =
            ViewModelProvider(this).get(DeliveryViewModel::class.java)

        _binding = FragmentDeliveryBinding.inflate(inflater, container, false)
        val root: View = binding.root

        val customer = resources.getStringArray(R.array.customer_list)
        val arrayAdapter = ArrayAdapter(requireContext(), R.layout.dropdown_item, customer)
        binding.autoCompleteTextView.setAdapter(arrayAdapter)
        return root
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContectView(com.example.delivery.R.layout.content_main)
        customerSign = findViewById(com.example.delivery.R.id.button_customer_sign) as Button
        customerSign.setOnClickListener(View.OnClickListener { openSignFragment() })
    } //This entire method is an error

    fun openSignFragment() {
        val intent = Intent(this, SignFragment::class.java)
        startActivity(intent)
    } //This entire method is an error

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

The listener and open arguments are just error after error and so are the last 3 imports of this .kt file. I need to find this out as I still have numerous other buttons to try and work out.

Please can someone assist me, thank you in advance.

Solution

Intents require an activity instance and class name, and fragments require the host container activity to be running. If u go to another activity, u will have to use this again to display your fragment in the activity container.

Please remove the setContentView() from your onCreate since you return the view in onCreateView of the fragment.

setContentView() populates the current window with the layout.

Fragment views are displayed in a container inside activity’s window.

only return your view once in onCreateView of your fragment, in your case, it is return root

Use this if u are using navigation component ->


 /** Please Execute this Code in onViewCreated method of your fragment **/

val navController = Navigation.findNavController(view)
navController.navigate(R.id.action_deliveryFragment_to_signFragment) 
//make sure you have defined the action in your navigation graph.

Or use this if you are not using navigation components ->

          val fragManager = requireActivity().supportFragmentManager
          val transaction = fragManager.beginTransaction()
          transaction.replace(
              R.id.container,
              RecordFragment()
          )
          transaction.addToBackStack(null) // if u want this fragment to stay in stack specify it
          transaction.commit()

Answered By – M Ashhal

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published