Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Is it bad practice to use SharedPreferences instead of onActivityResult for getting an object from one activity to another?

Posted on November 11, 2022 By

Solution 1 :

Amit pandey, i agree with that

In case if you want to pass data from activity 1->2 2->3 3->4 you use
simple intent if you want to use get data from 4->3 and 3->2 and 2->1
than you use startActivityForResult.

But, I think the shared preferences are commonly used to store any data that you want to restore after the app closes. if you’re going to share data while it’s open, you should consider the use of ViewModels instead

Solution 2 :

In case if you want to pass data from activity 1->2 2->3 3->4 you use simple intent if you want to use get data from 4->3 and 3->2 and 2->1 than you use startActivityForResult. But in case if you want to send or get data in any activity or Fragment its good if you use shared preference or local storage like sqlite

Solution 3 :

From my point of view, SharedPreference acts like local storage on your phone. It works well if you want to save some state like true or false, as simple as possible. Why? Because saving and get the data from SharedPreference quite painful.

Example use of SharedPreference. If you want to show a banner to the user, in a condition that the user never closes it before. So, if the user closes it before, it won’t be shown to the user. You can save that state whether the user had close it before or not in SharedPreference. That’s my perspective of using SharedPreference

Now, for onActivityResult. I usually use it if I want to get a variable that, usually, had modified in other activities. And that variable is important, for example, to keep update to the user about the data he/she been modified before.

READ  [FIXED] java - OnTouch giving different position, compared to ImageView position
Powered by Inline Related Posts

Example use of onActivityResult. Let’s say you move from activity A to activity B, which carried some value with it. In an activity B, the value is being modified. And when the user back to activity A, from activity B, it’s being carried too. That value is now used in activity A to show important info, which links to what user doing in activity B

Hope this answer helps you. Sorry for my bad grammar. Peace

Problem :

Closed. This question is opinion-based. It is not currently accepting answers.


Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 2 years ago.


Improve this question

It seems very inconvenient to make my object serializable or parcelable for onActivityResult resp. intent.getSerializableExtra() or intent.getParcelableExtra(). So I would easily store it into SharedPreferences.

Related to Android: Sending an object from one activity to other activity

Comments

Comment posted by a_local_nobody

why does it seem inconvenient

Comment posted by Kanwarpreet Singh

Depends upon your use case, If it’s not required further in any other activity then there is no need to allocate memory to it and store it into SharedPrefrence.

Comment posted by Rafsanjani

You can use a global object which is accessible by both activities.

Comment posted by EpicPandaForce

The real trick is that you don’t have to use 2 Activities for 2 screens.

Comment posted by Nicola Gallazzi

I agree with you, shared preferences should not be used to share not persistent data across the app. ViewModels with a shared activity context would be the right choice to share data across fragments

READ  [FIXED] java - I want to reduce image size when a pick a image from gallery or when take a picture
Powered by Inline Related Posts

Android Tags:android, android-intent, kotlin, sharedpreferences

Post navigation

Previous Post: Can a beginner learn Moonlight Sonata 1st Movement?
Next Post: [FIXED] java – App keeps crashing when ListView item is clicked on

Related Posts

[FIXED] kotlin – How to make chloropleth using Mapbox for Andriod, where the data comes in a GEOJSON Android
[FIXED] error when i use this comment Ionic cordova build android in ionic 9 Android
[FIXED] regex – IntelliJ: Regular expression to join multiple lines into single CSV line? Android
[FIXED] java – How to call JsonArrayRequest(response is an object array) for POST Method with a jsonobject body Android
[FIXED] mysql – android studio and sqlite count duplicate values Android
[FIXED] android – Select text to ellipsise in Jetpack Compose Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme