skip to Main Content

I don’t know how to pass data from the dialog fragment to the Activity. I have an Activity, which creates the Dialog. From this Dialog I want to pass Data to another Activity. Anyone know I can do this?

this is my 1st Activity:

class EinkaufslisteActivity : AppCompatActivity() {
//override val kodein by kodein()
//private val factory : EinkaufsViewModelFactory by instance()

@SuppressLint("NotifyDataSetChanged")
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_einkaufsliste)

    val database = EinkaufDatenbank(this)
    val repository = EinkaufsRepository(database)
    val factory = EinkaufsViewModelFactory(repository)


    val viewModel = ViewModelProviders.of(this, factory).get(EinkaufsViewModel::class.java)

    val adapter = ProduktAdapter(listOf(), viewModel)

    rvVorratsliste.layoutManager = LinearLayoutManager(this)
    rvVorratsliste.adapter = adapter

    viewModel.getAllProdukte().observe(this, Observer {
        adapter.items = it
        adapter.notifyDataSetChanged()
    })



    adapter.setOnItemClickListener {
        val produkt = it
        Intent(this, VorratslisteActivity::class.java).also {
            it.putExtra("EXTRA_PRODUKT", produkt)
        }
        EinkaufslisteProduktGekauftDialog(this, produkt, object : AddDialogListener{
            override fun onAddButtonClicked(produkt: Produkt) {

            }
            override fun onAddButtonClickedVorrat(produktVorrat: ProduktVorrat) {
                viewModel.delete(produkt)
                
            }
        }).show()
    }

This is my Dialog:

ass EinkaufslisteProduktGekauftDialog (context: Context, var produkt : Produkt?, var addDialogListener: AddDialogListener?) : AppCompatDialog(context){


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.dialog_einkaufsliste_produkt_gekauft)


    tvProduktgekauftName.text = produkt?.name.toString()
    etProduktGekauftAnzahl.hint = produkt?.anzahl.toString()


    btnProduktGekauftOk.setOnClickListener {
        val name = tvProduktgekauftName.text.toString()
        val anzahl = etProduktGekauftPreis.text.toString()
        val datum = etProduktGekauftDatum.text.toString()
        val preis = etProduktGekauftPreis.text.toString()

        if(name.isEmpty() || anzahl.isEmpty()){
           Toast.makeText(context, "Bitte fülle alle Felder aus", Toast.LENGTH_SHORT).show()
            return@setOnClickListener
        }


        val produktVorrat = ProduktVorrat(name, anzahl.toInt(), datum)
        addDialogListener?.onAddButtonClickedVorrat(produktVorrat)
        dismiss()
    }

This is my 2nd Activity:

class VorratslisteActivity : AppCompatActivity(){



override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_vorratsliste)



    val database = EinkaufDatenbank(this)
    val repository = VorratsRepository(database)
    val factory = VorratViewModelFactory(repository)


    val viewModel = ViewModelProviders.of(this, factory).get(VorratViewModel::class.java)

    val adapter = ProduktVorratAdapter(listOf(), viewModel)


    rvVorratsliste.layoutManager = LinearLayoutManager(this)
    rvVorratsliste.adapter = adapter

    viewModel.getAllProdukteVorratsliste().observe(this, Observer {
        adapter.items = it
        adapter.notifyDataSetChanged()
    })

    val produkt = intent.getSerializableExtra("EXTRA_PRODUKT") as? ProduktVorrat
    if(produkt != null) {
        viewModel.upsertVorrat(produkt)
    }

    btnVorratNeuesProdukt.setOnClickListener {
        VorratProduktHinzufuegenDialog(this,
            object : AddDialogListener {
                override fun onAddButtonClicked(produkt: Produkt) {
                    TODO("Not yet implemented")
                }

                override fun onAddButtonClickedVorrat(produktVorrat: ProduktVorrat) {
                viewModel.upsertVorrat(produktVorrat)
            }
        }).show()
    }

The "produkt" in activity 2 is null and i don’t know why

2

Answers


  1. ActivityA launches Dialog

    Dialog passes result back to ActivityA

    ActivityA launches ActivityB passing result from Dialog

    Login or Signup to reply.
  2. Since you are already using a ViewModel in your code, add a LiveData variable in your view model and set that live data on the Dialog.

    To get the value of the live data from another activity, ensure that you are using the same view model instance (using the activity view model factory). Then, you can access that view model (and live data) from that activity.

    In this way, you have a single source of data that is shared between multiple ui components (activity, fragment, dialogs)

    Check the official docs for Live Data here: https://developer.android.com/topic/libraries/architecture/livedata

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search