last day (25 days later) » 

3:32 PM
0
A: How to pass data or add observer at onBackPressed event in FragmentActivity

Yavor MitevI am missing something. Is viewModel and firstViewModel the same object? Also if so are you sure that you are creating the ViewModel of the Activity, but not the Fragment? mViewModel = ViewModelProviders.of(requireActivity()).get(YourViewModel.class);

 
That code is what i did to declear myViewModel. but i don't get observer event.
 
I don't understand what do you mean by "that" code and "declare". If you mean my code it is not declaration. Here I create and instance. If you mean yours - you never confirmed how you create the ViewModel and the names just do not add up. Nor the method names nor nothing. How you directly call setValue(5), but then you have also: fun setCurrencyVal(currencyVal: Int)
 
Sorry for the confusion. I created ViewModel class and used this ViewModel class in two fragments. I used by to create myViewModel variable. I updated my question
Could you please review?
 
Everything looks good. Just debug it. You can put logs in the init of both fragments with this reference. You can print also the VMs before yoiu call them so you can see that you are calling the same object. You can also print where you call setValue to check if you call it. And I am not sure how everything is working, but for example if the first fragment is removed if you replace it with the second fragment obviously it won't work. Also LiveData is lifecycle aware. If the second Fragment is on top of the first you won't be called either. Print the lifecycle methods of Fragment 1.
 
I replaced Fragment 1 to Fragment 2(with fragmentManger.begintransaction.replace()....) . So you mean viewmodel observer doesn't work in this case? If so could you please tell me alternative solution?
 
3:32 PM
Why you want to receive event in something that is gone?
 
Hello, @yavor, First of all, I would like to thank you for your effort for helping me fix bugs.
I wanna receive event at Fragment 1 when back button is pressed in Fragment 2 (Exactly there's value in Fragement 2 that has to be passed to Fragment 1 when back button is pressed in Fragment 2)
Hope this helps you understand more
 
give me 10min
 
No Problem. Thank you very much. I appreciate you help.
 
Here?
 
Yes
 
3:46 PM
First I am not sure how aware are you how LiveData is working
?
Want me to explain?
 
Honestly, I'm new to ViewModel and LiveData
That would be helpful, But I can find tutorial on Google
It's too much help
One thing i want to know from you is how to pass event & data when back button is pressed in Fragment 2
 
I am not sure if you design is correct
If you know how AcyncTask is working for example
 
sorry but not sure what you mean
 
The issue there was that you can receive a callback when the Activity is dead and you can try to show a dialog fro example and it will lead to a crash as the Activity is in the right state.
 
but it's same activity and i only replaced fragment2 from fragment 1
 
3:49 PM
LiveData handles such cases and that is why you pass a LifeCycleOnwer
If you are not in a right state there is no point the LiveData to send you something
 
Hmm...
so what do you suggest?
 
When Fragment 1 is gone it state is not right for observing anything
And even if you stack it second on to of first : same
If you log it, probably it will be even destroyed, Fragment 1
 
hm.. i see
 
And don't understand your use case
If something needs to happen to the UI you need to have a UI , and Fragment 1 is gone
If you don't have a UI, probably you don't need to call Fragment 1
or there is another option
 
Well, I need to update fragment1 UI depending on data coming from Fragment 2when back button is pressed in Fragment 2
 
3:53 PM
You add Fragment 2, on top of Fragment 1, and when fragment 2 is gone, Fragment 1 will receive the event
Or you can make the Activity do the work
 
supportFragmentManager.beginTransaction()
.apply(animationBlock)
.add(android.R.id.content, fragment, tag)
.addToBackStack(null)
.commit()
this is my code for replacing fragment
 
ok, so add it to the back stack, don't replace it
 
Hmm.. sorry but not sure what you mean.
DO you have skype and teamviewer?
 
.addToBackStack(null)
Do you do this also?
 
Of course, you don't need to help but help willbe much apprecaited
yes
 
3:57 PM
Can you show me the code where you create both fragments
 
Maybe you can check with teamviewer?
it would be much comfortable i guess
 
I need to install it. wait
 
ok, thanks
What communication tool do you use? I wanna be your friend. You are really friendly. :)
 
jav0r87
skype
 
sent contact requiest
 

last day (25 days later) »