https://kotlinlang.org logo
#android
Title
# android
o

Ofir Bar

02/24/2020, 9:08 AM
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

Francisco Javier Ruiz Rodriguez

02/24/2020, 9:37 AM
Maybe, in
onResume
of
UserFragment
, get info of user in
SharedPreferences
is the best option
🍻 1
o

Ofir Bar

02/24/2020, 9:53 AM
@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

Hitender Pannu

02/24/2020, 11:03 AM
@Ofir Bar what about onViewCreated?
o

Ofir Bar

02/24/2020, 1:16 PM
@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

Hitender Pannu

02/25/2020, 5:46 AM
Are you sure, because if onStart is getting called that means view was destroyed and onViewCreated should get called.
b

bhatnagarm

02/25/2020, 2:24 PM
@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

Ofir Bar

02/26/2020, 11:45 AM
@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

Cấn Văn Nghị

03/02/2020, 2:03 AM
I think starting
EditUserActivity
with
startActivityForResult
is a good way.
6 Views