I want to send an editText string from a dialogFragment popup to the activity screen behind it. I'm not sure how to do this in Kotlin since most resources online are in Java. I've tried to implement: How to send data from dialog to my activity kotlin?. However, I couldn't quite get it work so I'm not sure what method to use from here.
Here is the SelectAPScreen.kt (the activity I want the string to go to)
package com.wcsng.dlocapp
import android.annotation.SuppressLint
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import android.widget.*
import androidx.annotation.RequiresApi
import androidx.lifecycle.LifecycleEventObserver
import androidx.navigation.findNavController
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import kotlinx.android.synthetic.main.imu_data.*
import java.io.BufferedReader
import java.io.InputStreamReader
class SelectAPScreen : AppCompatActivity(), OnButtonClick {
@SuppressLint("ResourceAsColor")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_select_apscreen)
val directoryName = findViewById<EditText>(R.id.dir_field)
val pingIP = findViewById<EditText>(R.id.ip_field)
val rpiIP = findViewById<EditText>(R.id.rpiField)
val qtnIPs = {}
val addQtn = findViewById<Button>(R.id.addQtn)
addQtn.setOnClickListener {
var dialog = AddQtnFragment()
dialog.show(supportFragmentManager, "Add Qtn Popup")
}
}
}
Here is the DialogFragment Code:
package com.wcsng.dlocapp
import android.app.AlertDialog
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.annotation.NonNull
import androidx.fragment.app.DialogFragment
import androidx.navigation.fragment.findNavController
import com.wcsng.dlocapp.R
class AddQtnFragment: DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = AlertDialog.Builder(it)
// Get the layout inflater
val inflater = requireActivity().layoutInflater
val qtn_ip = view?.findViewById<EditText>(R.id.qtn_ip)?.text.toString()
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.add_qtn_popup, null))
.setPositiveButton("Save", DialogInterface.OnClickListener{dialog, id ->
// TODO Return the qtn_ip to SelectAPScreen
// Close the dialog and return back to the parent activity
dialog.dismiss()
})
.setNegativeButton("Cancel", DialogInterface.OnClickListener{dialog, id ->
dialog.dismiss()
})
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
}
CodePudding user response:
you can use getActivity() or requireActivity() methods for obtaing managing Activity, then you may cast it to proper class, e.g. (requireActivity() as SelectAPScreen) and now you can call methods of it
(requireActivity() as SelectAPScreen).selectAPScreenMethod()
selectAPScreenMethod() is a custom method placed inside SelectAPScreen class
