sedsiv
sedsiv

Reputation: 547

Pass parcelable arraylist from MainActivity to Fragment

I want to pass an arraylist of objects from my MainActivity to my OverviewFragment. The class is as follows.

@Parcelize
data class User(
    val id: String? = null,
    val firstName: String? = null,
    val lastName: String? = null,
    val email: String? = null,
    val WSC_Member: Boolean? = false,
    val Landesliga: Boolean? = false,
    val Superuser: Boolean? = false
) : Parcelable {

    fun getName(): String? {
        return "$firstName $lastName"
    }

}

Inside the MainActivity's onCreateView I get all users via the function getUsersData and pass it to the bundle

val overviewFragment: Fragment = OverviewFragment()
val fragmentManager: FragmentManager = supportFragmentManager
val data = Bundle()

// Get all users
GlobalScope.launch(Dispatchers.IO) {

    val allPlayers = getUsersData(userDocs)
    data.putParcelableArrayList("allPlayers", allPlayers)
    overviewFragment.arguments = data

    fragmentManager.beginTransaction().replace(
        R.id.overviewLayout,
        overviewFragment,
        "overviewFragment"
    ).addToBackStack(null).commit()

}

The getUsersDatafunction:

suspend fun getUsersData(UserCollection: CollectionReference): ArrayList<User> {

    val snapshot = UserCollection.get().await()
    val allPlayers = arrayListOf<User>()

    snapshot.documents.forEach { playerI ->

        val userI = User(
            playerI.id,
            playerI.getString("firstName"),
            playerI.getString("lastName"),
            playerI.getString("email"),
            playerI.getBoolean("WSC_Member"),
            playerI.getBoolean("Landesliga"),
            playerI.getBoolean("Superuser")
        )

        allPlayers.add(userI)

    }

    return allPlayers

}

In the OverviewFragments onCreateView

    val data = arguments
    val allPlayers = data?.getParcelableArrayList<User>("allPlayers")
    val test1 = allPlayers?.count()
    Log.i(TAG, "Test1: $test1")

I try to get the data, but it stays null. What am I missing?

I/OverviewFragment: Test1: null

Upvotes: 2

Views: 142

Answers (1)

zOqvxf
zOqvxf

Reputation: 1579

This is not what you want. You shouldn't be passing this data between screens manually, instead it should be stored in a Repository, that is accessible to each screen in the app, and they can pull the data themselves. You could be passing an Id as argument to the Fragment, if you wanted it to pull data for a specific item.

Also, you're incorrectly using coroutines, they're not meant to be used from the Activity, and definitely not with the GlobalScope. You should use a ViewModel, which has a viewModelScope, whose lifecycle is tied to the lifecycle of the screen. The ViewModel also survives orientation changes. You'd access your Repository from the ViewModel, and the Fragment would then observe the data by using LiveData.

You can check out Google's Sunflower sample project that showcases these patterns.

For more information, check out the following links:

Upvotes: 2

Related Questions