简体   繁体   中英

How to pass data from DialogFragment to fragment in android / Kotlin

I am new to both Android app development and Kotlin. I need to pass time from a DialogFragment to update the text of a button on a fragment (which is a tab) when the user set the time. Any suggestion how this can be done. I have read about viewModel or using interface but I could not really implement them in my code.

Here is my fragment_weight.xml :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Weight">

    
    <Button
        android:id="@+id/btnTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Time"
        android:layout_gravity="top|left" />
    
</FrameLayout>

Here is my Weight.kt

package com.example.mytablayout

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment    
import android.widget.Button
import com.example.mytablayout.fragments.TimePickerFragment
import java.util.*    

lateinit var btnTime:Button

class Weight : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_weight, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val c = Calendar.getInstance()            
        val hour = c.get(Calendar.HOUR)
        val minute = c.get(Calendar.MINUTE)            

        btnTime = view.findViewById(R.id.btnTime)
        var btnTimeText = hour.toString() + ':' + minute.toString()
        btnTime.setText(btnTimeText)           

        btnTime.setOnClickListener {
            val newTimeFragment = TimePickerFragment()
            newTimeFragment.show(childFragmentManager, "timePicker")

        }
    }
}

and here is my TimePickerFragment.kt

package com.example.mytablayout.fragments

import android.app.TimePickerDialog
import android.app.Dialog
import android.os.Bundle
import android.text.format.DateFormat
import android.widget.TimePicker
import androidx.fragment.app.DialogFragment
import java.util.*

class TimePickerFragment: DialogFragment(), TimePickerDialog.OnTimeSetListener {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        
        val c = Calendar.getInstance()
        val hour = c.get(Calendar.HOUR)
        val minute = c.get(Calendar.MINUTE)
        val dView = TimePickerDialog(requireActivity(), this, hour, minute, DateFormat.is24HourFormat(activity))
        
        return dView
    }

    override fun onTimeSet(view: TimePicker, hour: Int, minute: Int) {
        // Do something with the date chosen by the user           

    }

}

I have read about viewModel or using interface but I could not really implement them in my code.

Did you read the documentation on communicating with fragments?

https://developer.android.com/guide/fragments/communicate#pass-parent-child

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM