简体   繁体   中英

Why can't I use an intent to transition back to a previous fragment in my Android app?

I make a transition from a fragment to an activity but I am unable to transition back to the fragment without my app crashing.

Here is my fragment code:

package com.riverstonetech.gositeuk.ui.scotland

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ProgressBar
import androidx.fragment.app.Fragment
import com.google.firebase.firestore.FirebaseFirestore
import com.riverstonetech.gositeuk.CountriesActivity
import com.riverstonetech.gositeuk.R
import com.riverstonetech.gositeuk.RegionActivity
import kotlinx.android.synthetic.main.fragment_scotland.*

class ScotlandFragment : Fragment() {

    // Access a Cloud Firestore instance
    val db = FirebaseFirestore.getInstance()
    lateinit var adapter : ArrayAdapter<String>

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_scotland, container, false)

        (requireActivity() as CountriesActivity).initializeCustomActionBar(R.drawable.scotland_flag, R.string.title_regions)

        var regions : ArrayList<String>

        val docRef = db.collection("UKSites").document("Scotland")

        val progressBar: ProgressBar = root.findViewById(R.id.regionsLoadingProgressBar)

        docRef.get()
            .addOnSuccessListener { document ->

                progressBar?.visibility = ProgressBar.VISIBLE

                if (document != null) {

                    regions = document.get("Regions") as ArrayList<String>

                    adapter = ArrayAdapter(requireContext(), R.layout.list_item, regions)

                    regionsListView.adapter = adapter

                    regionsListView.setOnItemClickListener { parent, view, position, id ->

                        val intent = Intent(activity!!, RegionActivity::class.java)
                        intent.putExtra("SUB_COUNTRY", regions[position])
                        startActivity(intent)


                    }

                    progressBar?.visibility = ProgressBar.GONE

                } else {
                    Log.d("Debug", "No such document")
                }
            }
            .addOnFailureListener { exception ->
                Log.d("Debug", "get failed with ", exception)
            }

        return root
    }
}

And here is the relevant code in my activity class RegionActivity :


    fun previousSubCountryListButtonClicked(view: View) {

        val intent: Intent = Intent(this, ScotlandFragment::class.java)
        startActivity(intent)

    }

Here is the error output in the logcat window:

2020-02-10 15:46:54.089 27008-27008/com.riverstonetech.gositeuk E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.riverstonetech.gositeuk, PID: 27008
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)

I don't have enough knowledge of fragments and activity to work out why this doesn't work so any help would be appreciated.

You are making this way more complicated than it needs to be.

To return from RegionActivity to the previous Activity/Fragment , you just need to call finish() . That will destroy RegionActivity and return to whatever Activity was underneath it, in the same state it was in when you launched RegionActivity .

我使用了finish()而不是尝试使用意图切换到片段。

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