Hi guys, I have 2 Activities, and 1 fragment insid...
# android
o
Hi guys, I have 2 Activities, and 1 fragment inside one of them. `MainActivity`: Contains a Fragment
UserFragment
, the fragment display data of a User, the data is fetched from the SharedPreferences.
EditUserActivity
:Edits the data of the user, and once editing is done, this activity calls
finish()
and we go back to
MainActivity
, which displays
UserFragment
When returning to
UserFragment
, the data displayed is the old data for the user (the unedited data) I can think of 2 ways to make the fragment display fresh data, can anyone think of a smarter way, or explain which way below is better in terms of design? 1. Make
UserFragment
start
EditUserActivity
with
startActivityForResult
, and return a success or failure code, if success we do some data refresh. 2. Make
UserFragment
terminate itself once we navigate to
EditUserActivity
, and make
EditUserActivity
launch
MainActivity
with
UserFragment
on a backpress or successful save. Even if there is a way that is tricky and should not be in production i would like to know as I want to expand my skills please, thank you! 🙂
3️⃣ 3
f
Maybe, in
onResume
of
UserFragment
, get info of user in
SharedPreferences
is the best option
🍻 1
o
@Francisco Javier Ruiz Rodriguez Genius my friend, btw it is preferrable to do it in
onStart
as there might be a phone call/etc and then there will be additional call to render the UI
h
@Ofir Bar what about onViewCreated?
o
@Hitender Pannu My logic was initially at
onViewCreated
, it didn’t worked, as if I return from
EditUserActivity
to
MainActivity
,
UserFragment
won’t call
onViewCreated
h
Are you sure, because if onStart is getting called that means view was destroyed and onViewCreated should get called.
b
@Ofir Bar you try using this library, so the idea is to have a shared viewModel between activities like it's there for fragments.
o
@Hitender Pannu I don’t agree that the View was destroyed, it was stopped, through a call to
onStop()
Only after that we have
onDestroyView()
and
onDestroy()
👍 1
c
I think starting
EditUserActivity
with
startActivityForResult
is a good way.