Can't share Viewmodel between Activity and Fragment: - android

Android Studio 3.6
Here my viewModel:
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModelProviders
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.*
class BluetoothPageViewModel(application: Application) : AndroidViewModel(application) {
private val isSearchingTableModeLiveData = MutableLiveData<Boolean>()
private val isInitModeLiveData = MutableLiveData<Boolean>()
private val errorMessageLiveData = MutableLiveData<String>()
private val toastMessageLiveData = MutableLiveData<String>()
fun isInitModeLiveData(): LiveData<Boolean> {
return isInitModeLiveData
}
fun isSearchingTableModeLiveData(): LiveData<Boolean> {
return isSearchingTableModeLiveData
}
fun getErrorMessageLiveData(): LiveData<String> {
return errorMessageLiveData
}
fun getToastMessageLiveData(): LiveData<String> {
return toastMessageLiveData
}
Here fragment the subscribe to this viewmodel and success call Observer.onChanged()
class BluetoothPageFragment : Fragment() {
private lateinit var bluetoothPageViewModel: BluetoothPageViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
dataBinding =
DataBindingUtil.inflate(inflater, R.layout.bluetooth_page_fragment, container, false)
val view = dataBinding.getRoot()
dataBinding.setHandler(this)
init()
return view
}
private fun init() {
val context = this.context
val viewViewModelProvider = ViewModelProviders.of(this)
bluetoothPageViewModel = viewViewModelProvider.get(BluetoothPageViewModel::class.java)
bluetoothPageViewModel.isInitModeLiveData().observe(this, // SUCCESS CALL
Observer<Boolean> { isInitMode ->
})
}
Here my activity the subscribe to this viewmodel and NOT call Observer.onChanged()
import androidx.lifecycle.ViewModelProviders
class QRBluetoothSwipeActivity : AppCompatActivity() {
private lateinit var bluetoothPageViewModel: BluetoothPageViewModel
private fun init() {
val viewViewModelProvider = ViewModelProviders.of(this)
bluetoothPageViewModel = viewViewModelProvider.get(BluetoothPageViewModel::class.java)
val customFragmentStateAdapter = CustomFragmentStateAdapter(this)
customFragmentStateAdapter.addFragment(QrPageFragment())
bluetoothPageFragment = BluetoothPageFragment()
customFragmentStateAdapter.addFragment(bluetoothPageFragment)
dataBinding.viewPager2.adapter = customFragmentStateAdapter
initLogic()
}
private fun initLogic() {
dataBinding.viewPager2.registerOnPageChangeCallback(object :
ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
positionObservable.set(position)
}
})
bluetoothPageViewModel.getToastMessageLiveData() // this not call
.observe(this,
Observer<String> { message ->
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
})
}
Why not call getToastMessageLiveData() ?

In both cases you are using
ViewModelProviders.of(this)
It implifies you want this viewmodel with different scopes. One from Activity Scope and one from Fragment scope. If you want to share it.
If you want to share viewmodel you have to use single scope. I recommend using scope of bigger element, in this case activity.
In fragment you should call
ViewModelProviders.of(activity)
This should fix your issue.

you call ViewModelProviders.of(this) in both activity and fragment, but that is different contexts. So in your case, you instantiate 2 different instances of BluetoothPageViewModel, therefore onChanged callback is not called.
In order to share one instance between activity and a Fragment you should obtain viewModelProvider from the same context.
In your activity:
ViewModelProviders.of(this)
In your fragment:
ViewModelProviders.of(activity) or
activity?.let {
val bluetoothPageViewModel = ViewModelProviders.of(it).get(BluetoothPageViewModel::class.java)
bluetoothPageViewModel.isInitModeLiveData().observe(this, // SUCCESS CALL
Observer<Boolean> { isInitMode ->
})
}

Related

How can I send a variable from a fragment to a view model in MVVM architecture in kotlin?

Well I am a beginner with android and kotlin so I have been trying to send a variable semesterSelected from the fragment ViewCourses to my viewmodel UserViewModel is the codes are down below.
`class ViewCourses(path: String) : ReplaceFragment() {
private var semesterSelected= path
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
container?.removeAllViews()
return inflater.inflate(R.layout.fragment_view_courses, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
userRecyclerView = view.findViewById(R.id.recyclerView)
userRecyclerView.layoutManager = LinearLayoutManager(context)
userRecyclerView.setHasFixedSize(true)
adapter = MyAdapter()
userRecyclerView.adapter = adapter
makeToast(semesterSelected)
// The variable I am trying to send to UserViewModel is -->> semesterSelected
var viewModel: UserViewModel = ViewModelProvider(this)[UserViewModel::class.java]
viewModel.allUsers.observe(viewLifecycleOwner) {
adapter.updateUserList(it)
}
}
}
class UserViewModel : ViewModel() {
private val repository: UserRepository = UserRepository("CSE/year3semester1").getInstance()
private val _allUsers = MutableLiveData<List<CourseData>>()
val allUsers: LiveData<List<CourseData>> = _allUsers
init {
repository.loadUsers(_allUsers)
}
}
The reason I am doing this is I am wanting a to send a variable to my repository UserRepository all the way from ViewCourses and thought sending this via UserViewModel might be a way .
class UserRepository(semesterSelected: String) {
// The variable I am expecting to get from UserViewModel
private var semesterSelected = semesterSelected
private val databaseReference: DatabaseReference =
FirebaseDatabase.getInstance().getReference("course-list/$semesterSelected")
#Volatile
private var INSTANCE: UserRepository? = null
fun getInstance(): UserRepository {
return INSTANCE ?: synchronized(this) {
val instance = UserRepository(semesterSelected)
INSTANCE = instance
instance
}
}
fun loadUsers(userList: MutableLiveData<List<CourseData>>) {
databaseReference.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
try {
val courseList: List<CourseData> = snapshot.children.map { dataSnapshot ->
dataSnapshot.getValue(CourseData::class.java)!!
}
userList.postValue(courseList)
} catch (e: Exception) {
}
}
override fun onCancelled(error: DatabaseError) {
}
})
}
}
I tried something like below
class ViewCourses(path: String) : ReplaceFragment() {
private var semesterSelected= path
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
container?.removeAllViews()
return inflater.inflate(R.layout.fragment_view_courses, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
userRecyclerView = view.findViewById(R.id.recyclerView)
userRecyclerView.layoutManager = LinearLayoutManager(context)
userRecyclerView.setHasFixedSize(true)
adapter = MyAdapter()
userRecyclerView.adapter = adapter
makeToast(semesterSelected)
**// Sending the variable as parameter**
var viewModel: UserViewModel = ViewModelProvider(this)[UserViewModel(semesterSelected)::class.java]
viewModel.allUsers.observe(viewLifecycleOwner) {
adapter.updateUserList(it)
}
}
}
class UserViewModel(semesterSelected: String) : ViewModel() {
private val repository: UserRepository = UserRepository("CSE/year3semester1").getInstance()
private val _allUsers = MutableLiveData<List<CourseData>>()
val allUsers: LiveData<List<CourseData>> = _allUsers
init {
repository.loadUsers(_allUsers)
}
}
but doing this my app crashes . how can this be done ?
Thanks in Advance.
var viewModel: UserViewModel = ViewModelProvider(this)[UserViewModel(semesterSelected)::class.java]
UserViewModel(semesterSelected)::class.java NOR UserViewModel::class.java is a constructor for the view model.
If you would want to have ViewModel with that NEEDS initial parameters, you will have to create your own factory for that - which is a tad more complicated and for your case, it might be overkill for what you are trying to do but in the longterm it will pay off(Getting started with VM factories).
With that said, your needs can be easily solved by one function to initialize the view model.
class UserViewModel() : ViewModel() {
private lateinit var repository: UserRepository
private val _allUsers = MutableLiveData<List<CourseData>>()
val allUsers: LiveData<List<CourseData>> = _allUsers
fun initialize(semesterSelected: String) {
repository = UserRepository("CSE/year3semester1").getInstance()
repository.loadUsers(_allUsers)
}
}
A ViewModel must be created using a ViewModelProvider.Factory. But there is a default Factory that is automatically used if you don't specify one. The default factory can create ViewModels who have constructor signatures that are one of the following:
empty, for example MyViewModel: ViewModel.
saved state handle, for example MyViewModel(private val savedStateHandle: SavedStateHandle): ViewModel
application, for example MyViewModel(application: Application): AndroidViewModel(application)
both, for example MyViewModel(application: Application, private val savedStateHandle: SavedStateHandle): AndroidViewModel(application)
If your constructor doesn't match one of these four above, you must create a ViewModelProvider.Factory that can instantiate your ViewModel class and use that when specifying your ViewModelProvider. In Kotlin, you can use by viewModels() for easier syntax. All the instructions for how to create your ViewModelFactory are here.

How to pass data from adapter to fragment?

I've been trying to pass data(the email and phone of a user) from my adapter to my fragment. From what I've read online I should use a interface for this but I cant the data into my fragment still. Can anyone explain in steps how I should add a interface and how to put data into my interface from my adapter so I can call it in my fragment. Or is there another way to pass data from my adapter to my fragment. Below are my adapter and my fragment.
Adapter:
package ie.wit.savvytutor.adapters
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import ie.wit.savvytutor.R
import ie.wit.savvytutor.activity.MainActivity
import ie.wit.savvytutor.fragments.ViewChatFragment
import ie.wit.savvytutor.models.UserModel
class UserAdapter(private val userList: ArrayList<UserModel>, val context: Context) :
RecyclerView.Adapter<UserAdapter.UserViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
val itemView =
LayoutInflater.from(parent.context).inflate(R.layout.user_layout, parent, false)
return UserViewHolder(itemView)
}
class UserViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val username: TextView = itemView.findViewById(R.id.userNameView)
}
override fun onBindViewHolder(holder: UserViewHolder, position: Int, ) {
val currentItem = userList[position]
holder.username.text = currentItem.email
holder.itemView.setOnClickListener {
println(currentItem)
val optionsFrag = ViewChatFragment()
(context as MainActivity).getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, optionsFrag, "OptionsFragment").addToBackStack(
null
)
.commit()
}
}
override fun getItemCount(): Int {
return userList.size
}
}
Fragment
package ie.wit.savvytutor.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.Nullable
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.*
import ie.wit.savvytutor.R
import ie.wit.savvytutor.adapters.UserAdapter
import ie.wit.savvytutor.models.UserModel
class TutorChatFragment : Fragment() {
private lateinit var userRecyclerView: RecyclerView
private lateinit var userArrayList: ArrayList<UserModel>
private lateinit var dbRef: DatabaseReference
private lateinit var mAuth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
dbRef = FirebaseDatabase.getInstance("DATABASE LINK").getReference("Users").ref
mAuth = FirebaseAuth.getInstance()
}
#Nullable
override fun onCreateView(
inflater: LayoutInflater,
#Nullable container: ViewGroup?,
#Nullable savedInstanceState: Bundle?
): View {
//inflate the fragment layout
val root = inflater.inflate(R.layout.tutor_chat_fragment, container, false)
userRecyclerView = root.findViewById(R.id.userListView)
userRecyclerView.layoutManager = LinearLayoutManager(context)
userRecyclerView.setHasFixedSize(true)
userArrayList = arrayListOf<UserModel>()
getUser()
return root
}
private fun getUser() {
userArrayList.clear()
dbRef.addValueEventListener(object: ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
for (postSnapshot in snapshot.children) {
val currentUser = postSnapshot.getValue(UserModel::class.java)
//BUG FIX 1.26.13
val email = currentUser?.email
if (email != null) {
userArrayList.add(currentUser)
}
userRecyclerView.adapter?.notifyDataSetChanged()
userRecyclerView.adapter = context?.let { UserAdapter(userArrayList, it) }
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
}
If you want to use an interface, you just need to define one with a function to receive your data, make the fragment implement it, then pass the fragment to the adapter as an implementation of that interface:
data class UserData(val email: String, val phone: String)
class UserAdapter(
private val userList: ArrayList<UserModel>,
val context: Context,
val handler: UserAdapter.Callbacks // added this here, so you're passing it in at construction
) : RecyclerView.Adapter<UserAdapter.UserViewHolder>() {
...
private fun doWhatever(email: String, phone: String) {
// pass the data to the handler (which will probably be your Fragment)
handler.handleUserData(UserData(email, phone))
}
// nested inside the UserAdapter class to keep things tidy
interface Callbacks {
fun handleUserData(data: UserData)
}
}
Then in the Fragment:
// add the Callbacks interface type
class TutorChatFragment : Fragment(), UserAdapter.Callbacks {
override fun onCreateView(
inflater: LayoutInflater,
#Nullable container: ViewGroup?,
#Nullable savedInstanceState: Bundle?
): View {
...
userRecyclerView.layoutManager = LinearLayoutManager(context)
// set up the adapter here, passing this fragment as the Callbacks handler
userRecyclerView.adapter = UserAdapter(userArrayList, context, this)
...
}
// interface implementation
override fun handleUserData(data: UserData) {
// whatever
}
}
And that's it. You're not hardcoding a dependency on that particular Fragment type, just the interface, and this fragment implements it so it can pass itself.
A more Kotliny way to do it is to ignore interfaces and just pass a function instead
class UserAdapter(
private val userList: ArrayList<UserModel>,
val context: Context,
val handler: (UserData) -> Unit // passing a function that takes a UserData instead
) : RecyclerView.Adapter<UserAdapter.UserViewHolder>() {
...
private fun doWhatever(email: String, phone: String) {
// call the handler function with your data (you can write handler.invoke() if you prefer)
handler(UserData(email, phone))
}
}
// no interface this time
class TutorChatFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
#Nullable container: ViewGroup?,
#Nullable savedInstanceState: Bundle?
): View {
...
userRecyclerView.layoutManager = LinearLayoutManager(context)
// pass in a handler function
userRecyclerView.adapter = UserAdapter(userArrayList, context) { userData ->
handleUserData(userData)
}
// or if you're just passing it to that function down there,
// you could do UserAdapter(userArrayList, context, ::handleUserData)
// and pass the function reference
...
}
// might be convenient to still do this in its own function
private fun handleUserData(data: UserData) {
// whatever
}
}
Ideally you should be doing what I've done there - create the adapter once during setup, and have a function on it that allows you to update it. Your code creates a new one each time you get data. You do this the same way in both though
Your other option is using a view model that the adapter and fragment both have access to, but this is how you do the interface/callback approach
Actually there is one very easy way to get data from your adapter in to your fragment or activity. It is called using Higher Order Functions.
In your adapter
Add higher order function in your adapter.
class UserAdapter(private val userList: ArrayList<UserModel>, val context: Context) :
RecyclerView.Adapter<UserAdapter.UserViewHolder>() {
//your rest of the adapter's code
private var onItemClickListener:((UserModel)->Unit)? = null
fun setOnItemClickListener(listener: (UserModel)->Unit) {
onItemClickListener = listener
}
}
In Your UserViewHolder
val rootView = itemView.rootView
In Your onBindViewHolder
set a click listener on rootView
holder.rootView.setOnClickListener {
onItemClickListener?.let{
it(currentItem)
}
}
In Your Fragment
//create the instance of UserAdapter
userAdapter.setOnItemClickListener {
//here you have your UserModel in your fragment, do whatever you want to with it
}
And, a suggestion in the last. Start using ViewBinding, it will save you from a lots of hectic work.

Android: Livedata Observer gets never called, recylerview list is never submitted, navgraphviewmodel

I have a ShopFilterFragmentProductFilter which is inside a ShopFilterFragmentHolder which itself holds a ViewPager2. This ShopFilterFragmentHolder is a DialogFragment which is opened inside my ShopFragment. So ShopFragment -> ShopFilterFragmentHolder (Dialog, ViewPager2) -> ShopFilterFragmentProductFilter. ALL of these Fragments should share the same navgraphscoped viewmodel.
The problem I have is, that when I attach an observer inside my ShopFilterFragmentProductFilter to get my recyclerview list from cloud-firestore, this observer never gets called and therefore I get the error message "No Adapter attached, skipping layout". I know that this is not a problem with how I instantiate and assign the adapter to my recyclerview, because when I set a static list (e.g creating a list inside my ShopFilterFragmentProductFilter) everything works.
Why do I don't get the livedata value? To my mind, there is a problem with the viewmodel creation.
Here is my current approach:
ShopFilterFragmentProductFilter
#AndroidEntryPoint
class ShopFilterFragmentProductFilter : Fragment() {
private var _binding: FragmentShopFilterItemBinding? = null
private val binding: FragmentShopFilterItemBinding get() = _binding!!
private val shopViewModel: ShopViewModel by navGraphViewModels(R.id.nav_shop) { defaultViewModelProviderFactory }
#Inject lateinit var shopFilterItemAdapter: ShopFilterItemAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentShopFilterItemBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
bindObjects()
submitAdapterList()
}
override fun onDestroyView() {
super.onDestroyView()
binding.rvShopFilter.adapter = null
_binding = null
}
private fun bindObjects() {
with(binding) {
adapter = shopFilterItemAdapter
}
}
private fun submitAdapterList() {
shopViewModel.shopProductFilterList.observe(viewLifecycleOwner) {
shopFilterItemAdapter.submitList(it)
shopFilterItemAdapter.notifyDataSetChanged()
toast("SUBMITTED LIST") // this does never get called
}
/* // this works
shopFilterItemAdapter.submitList(
listOf(
ShopFilterItem(0, "ITEM 1"),
ShopFilterItem(0, "ITEM 2"),
ShopFilterItem(0, "ITEM 3"),
ShopFilterItem(0, "ITEM 4"),
ShopFilterItem(0, "ITEM 5"),
)
)
*/
}
}
ViewModel
class ShopViewModel #ViewModelInject constructor(
private val shopRepository: ShopRepository,
private val shopFilterRepository: ShopFilterRepository
) : ViewModel() {
private val query = MutableLiveData(QueryHolder("", ""))
val shopPagingData = query.switchMap { query -> shopRepository.search(query).cachedIn(viewModelScope) }
val shopProductFilterList: LiveData<List<ShopFilterItem>> = liveData { shopFilterRepository.getProductFilterList() }
val shopListFilterList: LiveData<List<ShopFilterItem>> = liveData { shopFilterRepository.getListFilterList() }
fun search(newQuery: QueryHolder) {
this.query.value = newQuery
}
}
ShopFilterRepositoryImpl
class ShopFilterRepositoryImpl #Inject constructor(private val db: FirebaseFirestore) : ShopFilterRepository {
override suspend fun getProductFilterList(): List<ShopFilterItem> = db.collection(FIREBASE_SERVICE_INFO_BASE_PATH)
.document(FIREBASE_SHOP_FILTER_BASE_PATH)
.get()
.await()
.toObject<ShopFilterItemHolder>()!!
.productFilter
override suspend fun getListFilterList(): List<ShopFilterItem> = db.collection(FIREBASE_SERVICE_INFO_BASE_PATH)
.document(FIREBASE_SHOP_FILTER_BASE_PATH)
.get()
.await()
.toObject<ShopFilterItemHolder>()!!
.listFilter
}
Nav_graph
Probably, you should define it as MutableLiveData:
private val shopProductFilterList: MutableLiveData<List<ShopFilterItem>> = MutableLiveData()
And in a method in your viewModel that gets the data through repository, you should post the LiveData value:
fun getProductFilterList() = viewModelScope.launch {
val dataFetched = repository. getProductFilterList()
shopProductFilterList.postValue(dataFetched)
}

Why is a property inside aFragment, which is in a ViewPager after showing a Dialog not initialized any more?

As you can see in the images I have a TabLayout and a Viewpager.
I have one Fragment for Products and one for Categories. In my onViewCreated Method I am setting the progressBar visible. The progressBar property is initialized in the onViewCreated Method before accessing the ProgressBar.
As you can see in the image the ProgressBar is set successfully to visible. The products are loaded and afterwards the ProgressBar is set invisible again.
When I hit the Floating Action Button I am opening a Dialog, which is extending the DialogFragment class.
The Main Class, which is containing the TabView and the ViewPager is implementing the ProductDialog.ProductDialogEventListener. This means, when I am hitting the save Button the Method onProductAdded is called in the Main Class.
This Method however is calling a Method in the productFragment Class to add the new Product to the list inside the fragment. When the ProgressBar is set visible now, the I receive a UninitializedPropertyAccessException because the progressBar Property is not initialized.
I don't understand it, because I have already initialized and used it before.
Here is some code:
The ProductDialog:
class ProductDialog : DialogFragment() {
lateinit var editTextTitle: EditText
lateinit var editTextSpecialText: EditText
lateinit var editTextDescription: EditText
lateinit var radioButtonMen: RadioButton
lateinit var radioButtonWomen: RadioButton
lateinit var radioButtonUnisex: RadioButton
lateinit var productDialog: AlertDialog
lateinit var productDialogEventListener: ProductDialogEventListener
lateinit var apiOperations: ApiOperations
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(activity)
val inflater = requireActivity().layoutInflater
val view = inflater.inflate(R.layout.layout_product_dialog,null)
builder.setView(view)
.setTitle("Produkt erstellen")
.setPositiveButton("Speichern") { _, _ -> saveProductRequest()}
.setNegativeButton(R.string.cancel) { _, _ ->
dialog.cancel()
}
setViews(view)
setValues(view)
productDialog = builder.create()
return productDialog
}
interface ProductDialogEventListener{
val onProductAdded: (variant:Product)->Unit
}
override fun onAttach(context: Context?) {
super.onAttach(context)
try {
productDialogEventListener = context as ProductDialogEventListener
}catch (exception: ClassCastException){
throw exception
}
}
private fun onProductSaved(id:String){
productDialogEventListener.onProductAdded(Product(id.toInt(),editTextTitle.text.toString(),editTextSpecialText.text.toString(),editTextDescription.text.toString(),getCheckedGender()))
}
The Main Activity (OverviewActivity)
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.TabLayout
import android.support.design.widget.TabLayout.OnTabSelectedListener
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.view.View
import android.widget.ListView
import android.widget.ProgressBar
import android.widget.TableLayout
import android.widget.Toast
class OverviewActivity : AppCompatActivity(), ProductDialog.ProductDialogEventListener, CategoryDialog.CategoryDialogListener {
private lateinit var listViewCategories: ListView
private lateinit var apiOperations: ApiOperations
private lateinit var progressBarCategories: ProgressBar
private lateinit var addCategoryButton: FloatingActionButton
private lateinit var tabLayout: TabLayout
private lateinit var viewPager : ViewPager
private var categories = ArrayList<Category>()
private val productsFragment = ProductsFragment()
private val categoriesFragment = CategoriesFragment()
override fun onResume() {
super.onResume()
setCategories()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_overview)
setViews()
setViewPager()
setValues()
}
private fun setViews(){
listViewCategories = findViewById(R.id.listViewCategories)
addCategoryButton = findViewById(R.id.add_category_button)
progressBarCategories = findViewById(R.id.progressCategories)
viewPager = findViewById(R.id.viewPager)
tabLayout = findViewById(R.id.tabLayout)
}
private fun setViewPager(){
val fragmentList = listOf(productsFragment,categoriesFragment)
val overViewPageViewAdapter = OverViewPageViewAdapter(supportFragmentManager,tabLayout.tabCount)
viewPager.adapter = overViewPageViewAdapter
viewPager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabLayout))
tabLayout.addOnTabSelectedListener(object : OnTabSelectedListener{
override fun onTabReselected(p0: TabLayout.Tab?) {
}
override fun onTabUnselected(p0: TabLayout.Tab?) {
}
override fun onTabSelected(tab: TabLayout.Tab) {
viewPager.currentItem = tab.position
}
})
}
private fun setValues(){
apiOperations = ApiOperations(applicationContext)
}
private fun setProgressBarVisible(visible: Boolean, progressBar: ProgressBar){
when(visible){
true -> progressBar.visibility = View.VISIBLE
else -> progressBar.visibility = View.GONE
}
}
fun addNewProductButtonClicked(view:View){
val dialog = ProductDialog()
dialog.show(supportFragmentManager,"New Product")
}
override val onProductAdded: (product: Product) -> Unit
get() = {product ->
productsFragment.onProductAdded(product)
}
}
And finally the productFragment Class
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ListView
import android.widget.ProgressBar
import android.widget.Toast
class ProductsFragment :Fragment(){
lateinit var apiOperations: ApiOperations
lateinit var listViewProducts: ListView
lateinit var addProductButton: FloatingActionButton
lateinit var progressBar: ProgressBar
private var products = ArrayList<Product>()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.products_fragment_layout,container,false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
apiOperations = ApiOperations(view.context)
setViews(view)
setProducts(view)
}
private fun setViews(view: View){
listViewProducts = view.findViewById(R.id.listViewProducts)
addProductButton = view.findViewById(R.id.add_product_button)
progressBar = view.findViewById(R.id.progress_circular)
}
private fun updateProductList(view: View,newList:ArrayList<Product>){
Toast.makeText(context,"settingProgressbar visible",Toast.LENGTH_SHORT).show()
setProgressBarVisible(true)
products = newList
listViewProducts.adapter = ProductListViewAdapter(view,
products,::deleteProduct)
listViewProducts.setOnItemClickListener{ _, _, position, _ ->
val product: Product = listViewProducts.adapter.getItem(position) as Product
openProductActivity(product)
}
setProgressBarVisible(false)
}
private fun setProducts(view: View){
setProgressBarVisible(true)
apiOperations.getProducts(
{ newList -> updateProductList(view,newList)},
{
setProgressBarVisible(false)
Toast.makeText(context,"Produkte konnten nicht geladen werden!",Toast.LENGTH_SHORT).show()
}
)
}
fun addNewProductButtonClicked(view:View){
val dialog = ProductDialog()
dialog.show(fragmentManager,"New Product")
}
private fun deleteProduct(view: View,productId: Int){
setProgressBarVisible(true)
apiOperations.deleteProduct(productId,{
products.removeAll { product -> product.id == productId }
updateProductList(view,products)
setProgressBarVisible(false)
Toast.makeText(view.context,"Produkt wurde gelöscht!", Toast.LENGTH_SHORT).show()
},
{
Toast.makeText(view.context, "Es ist ein Fehler aufgetreten!" , Toast.LENGTH_LONG).show()
setProgressBarVisible(false)
}
)
}
private fun openProductActivity(product: Product){
val intent = Intent(view?.context, ProductActivity::class.java)
intent.putExtra("product",product)
startActivity(intent)
}
private fun setProgressBarVisible(visible: Boolean){
if(progressBar==null){
progressBar = view!!.findViewById(R.id.progress_bar)
}
Toast.makeText(context,"setProgressbar called",Toast.LENGTH_SHORT).show()
when(visible){
true -> progressBar.visibility = View.VISIBLE
else -> progressBar.visibility = View.GONE
}
}
fun onProductAdded(product: Product) {
setProgressBarVisible(true)
products.add(product)
Toast.makeText(context,products.size.toString(),Toast.LENGTH_SHORT).show()
Toast.makeText(context,"Neues Produkt " + product.title + " hinzugefügt!",Toast.LENGTH_SHORT).show()
setProgressBarVisible(false)
}
}
Also I added following Code in the other Fragement. There I have the same issue. It seems like getView() (in Kotlin only view) results in null and hence is not working. Why is this happening. I also tried to reset the values in onResume as recommended in the comments but neither onPause or onResume is called after closing the DialogFragment.
I can't imagine that I am the only one having this problem.
fun onCategoryAdded(category: Category){
Log.i("fragment","onCategoryAdded called")
if(categories === null){
Log.i("fragment", "categories is null")
}
if(view == null){
Log.i("fragment", "view is null")
}
if(categories!=null && view!= null){
categories.add(category)
updateCategoriesList(view!!.context,categories)
}
}
You can create the progressbar inside your activity instead your fragment and then access the progressbar from both your fragments. By doing so, you won't face the error.
The error was accessing the two Fragments initialized at the beginning of the class.
private val productsFragment = ProductsFragment()
private val categoriesFragment = CategoriesFragment()
And that was very stupid, because these were not the fragments that were add to the ViewPager. In the ViewPagerAdapter the Fragments were created and added to the ViewPager.
Thats why I added two methods to the adapter which return the Fragment instances.
In the holding Activity I am calling those before accessing the fragment instances now. And the problem is solved.
The problem was sitting in front of the keyboard.
class OverViewPageViewAdapter(fragmentManager: FragmentManager, private var tabCount: Int) : FragmentStatePagerAdapter(fragmentManager) {
private var productFragment = ProductsFragment()
private var categoriesFragment = CategoriesFragment()
override fun getItem(position: Int): Fragment? {
return when (position) {
0 -> productFragment
1 -> categoriesFragment
else -> null
}
}
override fun getCount(): Int {
return tabCount
}
fun getCategoriesFragmentInstance(): CategoriesFragment{
return categoriesFragment
}
fun getProductFragmentInstance(): ProductsFragment{
return productFragment
}
}
override val onProductAdded: (product: Product) -> Unit
get() = {
product ->
val adapter: OverViewPageViewAdapter = viewPager.adapter as OverViewPageViewAdapter
adapter.getProductFragmentInstance().onProductAdded(product)
}

Cannot get the same instance of a scoped component - Dagger 2 Clean architecture

I'm using Dagger 2 in clean architecture project, I have 2 fragments. These 2 fragments should be scoped together to share the same instances, but unfortunately, I got empty object in the second fragment.
Application Component
#ApplicationScope
#Component(modules = [ContextModule::class, RetrofitModule::class])
interface ApplicationComponent {
fun exposeRetrofit(): Retrofit
fun exposeContext(): Context
}
Data layer - Repository
class MoviesParsableImpl #Inject constructor(var moviesLocalResult: MoviesLocalResult): MoviesParsable {
private val TAG = javaClass.simpleName
private val fileUtils = FileUtils()
override fun parseMovies() {
Log.d(TAG,"current thread is ".plus(Thread.currentThread().name))
val gson = Gson()
val fileName = "movies.json"
val jsonAsString = MyApplication.appContext.assets.open(fileName).bufferedReader().use{
it.readText()
}
val listType: Type = object : TypeToken<MoviesLocalResult>() {}.type
moviesLocalResult = gson.fromJson(jsonAsString,listType)
Log.d(TAG,"result size ".plus(moviesLocalResult.movies?.size))
}
override fun getParsedMovies(): Results<MoviesLocalResult> {
return Results.Success(moviesLocalResult)
}
}
Repo Module
#Module
interface RepoModule {
#DataComponentScope
#Binds
fun bindsMoviesParsable(moviesParsableImpl: MoviesParsableImpl): MoviesParsable
}
MoviesLocalResultsModule(the result need its instance across different fragments)
#Module
class MoviesLocalResultModule {
#DataComponentScope
#Provides
fun provideMovieLocalResults(): MoviesLocalResult{
return MoviesLocalResult()
}
}
Use case
class AllMoviesUseCase #Inject constructor(private val moviesParsable: MoviesParsable){
fun parseMovies(){
moviesParsable.parseMovies()
}
fun getMovies(): Results<MoviesLocalResult> {
return moviesParsable.getParsedMovies()
}
}
Presentation Component
#PresentationScope
#Component(modules = [ViewModelFactoryModule::class],dependencies = [DataComponent::class])
interface PresentationComponent {
fun exposeViewModel(): ViewModelFactory
}
First ViewModel, where I got the result to be shared with the other fragment when needed
class AllMoviesViewModel #Inject constructor(private val useCase: AllMoviesUseCase):ViewModel() {
private val moviesMutableLiveData = MutableLiveData<Results<MoviesLocalResult>>()
init {
moviesMutableLiveData.postValue(Results.Loading())
}
fun parseJson(){
viewModelScope.launch(Dispatchers.Default){
useCase.parseMovies()
moviesMutableLiveData.postValue(useCase.getMovies())
}
}
fun readMovies(): LiveData<Results<MoviesLocalResult>> {
return moviesMutableLiveData
}
}
Second ViewModel where no need to request data again as it's expected to be scoped
class MovieDetailsViewModel #Inject constructor(private val useCase: AllMoviesUseCase): ViewModel() {
var readMovies = liveData(Dispatchers.IO){
emit(Results.Loading())
val result = useCase.getMovies()
emit(result)
}
}
First Fragment, where data should be requested:
class AllMoviesFragment : Fragment() {
private val TAG = javaClass.simpleName
private lateinit var viewModel: AllMoviesViewModel
private lateinit var adapter: AllMoviesAdapter
private lateinit var layoutManager: LinearLayoutManager
private var ascendingOrder = true
#Inject
lateinit var viewModelFactory: ViewModelFactory
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
DaggerAllMoviesComponent.builder()
.presentationComponent(
DaggerPresentationComponent.builder()
.dataComponent(
DaggerDataComponent.builder()
.applicationComponent(MyApplication.applicationComponent).build()
)
.build()
).build()inject(this)
viewModel = ViewModelProvider(this, viewModelFactory).get(AllMoviesViewModel::class.java)
startMoviesParsing()
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_all_movies, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
setupRecyclerView()
viewModel.readMovies().observe(viewLifecycleOwner, Observer {
if (it != null) {
when (it) {
is Loading -> {
showResults(false)
}
is Success -> {
showResults(true)
Log.d(TAG, "Data observed ".plus(it.data))
addMoviesList(it.data)
}
is Error -> {
moviesList.snack(getString(R.string.error_fetch_movies))
}
}
}
})
}
Second Fragment, where I expect to get the same instance request in First Fragment as they are scoped.
class MovieDetailsFragment: Fragment() {
val TAG = javaClass.simpleName
#Inject
lateinit var viewModelFactory: ViewModelFactory
lateinit var viewModel: MovieDetailsViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val depend = DaggerAllMoviesComponent.builder()
.presentationComponent(
DaggerPresentationComponent.builder()
.dataComponent(
DaggerDataComponent.builder()
.applicationComponent(MyApplication.applicationComponent).build())
.build()
).build()
depend.inject(this)
viewModel = ViewModelProvider(this, viewModelFactory).get(MovieDetailsViewModel::class.java)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
viewModel.readMovies.observe(this, Observer {
if (it!=null){
Log.d(TAG,"Movies returned successfully")
}
})
return super.onCreateView(inflater, container, savedInstanceState)
}
}
Scopes tell a component to cache the results of a binding. It has nothing to do with caching instances of any components. As such, you are always creating a new DataComponent, PresentationComponent, and AllMoviesComponent in your fragments' onCreate methods.
In order to reuse the same AllMoviesComponent instance, you need to store it somewhere. Where you store it can depend on your app architecture, but some options include MyApplication itself, the hosting Activity, or in your navigation graph somehow.
Even after fixing this, you can't guarantee that parseMovies has already been called. The Android system could kill your app at any time, including when MoviesDetailFragment is the current fragment. If that happens and the user navigates back to your app later, any active fragments will be recreated, and you'll still get null.

Categories

Resources