Get data from argument of fragment

Viewed 176

How can I get data from the argument of the fragment? I call fragment from MainActivity from function RegistrationCallFragment()

fun RegistrationCallFragment(){
val extras = getIntent().getExtras();
    if(extras != null) {
        val bundle = Bundle()

        bundle.putString("phone", extras.getString("phone", ""))
        bundle.putString("email", extras.getString("email", ""))
        bundle.putString("password", extras.getString("password", ""))

        Registration_second.getNewInstance(args = bundle)

    }
}

And put all data in argument of frgament to companion object

    fun getNewInstance(args: Bundle): Registration_second{
            val registrationSecond = Registration_second()
            registrationSecond.arguments = args
            return  registrationSecond
        }
    }

How I can get this data from a companion object?

So, I find some things and do it in my application in this function

fun RegistrationCallFragment(){

    fun RegistrationCallFragment(){
        //val extras = getIntent().getExtras();
        //if(extras != null) {
            //val s =extras?.getString("phone", "")
            val bundle = Bundle()
            bundle.putString("phone",
                FragmentManager.findFragment<SecondFragment>(View(this)).view?.findViewById<EditText>(R.id.phone_signup)?.text.toString()
            )
            bundle.putString("email",
                FragmentManager.findFragment<SecondFragment>(View(this)).view?.findViewById<EditText>(R.id.email_signup)?.text.toString()
            )
            bundle.putString("password",
                FragmentManager.findFragment<SecondFragment>(View(this)).view?.findViewById<EditText>(R.id.pasword_signup)?.text.toString()
            )

            Registration_second.getNewInstance(args = bundle)


        //}
    }

and this

bundle.putString("phone",
                FragmentManager.findFragment<SecondFragment>(View(this)).view?.findViewById<EditText>(R.id.phone_signup)?.text.toString()
            )

crash my app. What I do incorrectly?

Cod in MainActivity

        import android.content.Intent
    import android.os.Bundle
    import android.util.Log
    import android.view.View
    import android.widget.EditText
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import androidx.fragment.app.FragmentManager
    import androidx.navigation.fragment.NavHostFragment.findNavController
    import androidx.navigation.fragment.findNavController


    class MainActivity : AppCompatActivity() {

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
        }
        fun toastMe(Str: String) {
            val myToast = Toast.makeText(this,Str, Toast.LENGTH_SHORT)
            myToast.show()
        }
        fun RegistrationCallFragment(){

                val bundle = Bundle()
                bundle.putString("phone",
                    FragmentManager.findFragment<SecondFragment (View(this)).view?.findViewById<EditText>(R.id.phone_signup)?.text.toString()
            )
                bundle.putString("email",
                    FragmentManager.findFragment<SecondFragment>(View(this)).view?.findViewById<EditText>(R.id.email_signup)?.text.toString()
                )
                bundle.putString("password",
                    FragmentManager.findFragment<SecondFragment>(View(this)).view?.findViewById<EditText>(R.id.pasword_signup)?.text.toString()
                )

                Registration_second.getNewInstance(args = bundle)
        }

    }

EditText in fragment_second.xml

    <EditText
            android:id="@+id/phone_signup"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:hint="@string/phone"
            android:textColorHint="@color/grey"
            android:textColor="@color/black"
            android:background="@drawable/input_text"
            android:layout_gravity="center"
            android:layout_marginBottom="85dp"
            android:gravity="center"
            android:ems="10"
            android:inputType="phone"
            />

Cod from fragment where I get data

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment

        //val extras = getIntent()?.getExtras();
        //if(extras != null) {
        //    val phone = extras.getString("phone", "")
        //    val email = extras.getString("email", "")
        //    val password = extras.getString("password", "")
        //return inflater.inflate(R.layout.fragment_registration_second, container, false)

        val phone = arguments?.getString("phone", "")
        val email = arguments?.getString("email", "")
        val password = arguments?.getString("password", "")
        Log.d("PHONE BLYAT", phone+email+password+"THis")
        print(phone+email+password)
        val activ = activity
        (activ as MainActivity).toastMe(phone.toString())
        return inflater.inflate(R.layout.fragment_registration_second, container, false)
    }

Log

enter image description here

1 Answers

On your Registration_second fragment onCreate method getData from argument like below code:

  override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {

                   val phone = it.getStringExtra("phone")
                   val email = it.getStringExtra("email")
                   val password = it.getStringExtra("passsword")
         }       
    }

Related