Skip to content

Snappy1

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

[FIXED] android – How to pass data by Back Button, Kotlin

Posted on November 11, 2022 By

Solution 1 :

You can start Activity2 using startActivityForResult and then call setResult to return a value.

Or you can use a ViewModel (common class) to share data between activities.
The difference between a using a variable in a ViewModel and saving data to SharedPreferences is that the variables in a ViewModel are not persisted and the information will be lost when the app is closed.

Solution 2 :

pass and get data into multiple activity for this i recommend you to use the shared preference to get and set the data into multiple activity. and you can also use to local storage. but if you are using 2 activity than you can use on activity Result .

Problem :

There are several answers on the site, but it looks like I need a more clear answer.

Assume Activity1 contains a TextView and Activity2 contains a RecyclerView. If some data on Activity2 is modified, Activity1 should be informed. There are two ways to Activity1 from Activity2:

  1. A normal button on the layout.
  2. “The” Back Button.

It looks possible to call startActivity() with putExtra() inside onBackPressed(). However this does not look efficient to me, as it will reload the UI, reload data, restart initial checks etc. Therefore, I’d like to send a flag so that “only the essential” changes can be made in Activity1 -by decoding the flag with an if statement in onStart() I suppose(?). How do I achieve this with both buttons.

Bonus question:

  1. It is possible to use sharedPreferences to transfer data.
  2. It is possible to use a common class to transfer data.

Is using a common class faster than sharedPreferences, especially if I need to transfer data often? I read something like: “once sharedPreferences is loaded into RAM it is in the RAM till the closing of the app, so it is not that slow.”
Also, is using common class robust enough to handle unexpected behavior like closing the app suddenly, or receiving a call etc.?

READ  [FIXED] Android Studio - rendering design not working properly after update
Powered by Inline Related Posts

Comments

Comment posted by Tenfour04

Search for startActivityForResult.

Comment posted by this

Thank you. What if user jumps to Activity3 from Activity2? Will

Comment posted by Juan Cruz Soler

You have to call

Comment posted by Xfce4

Actually I have a transfer structure that depends on

Comment posted by Xfce4

Thanks. I also used sharedPreferences. But everytime activity1 starts I load a list from sharedPreferences, which does not look so efficient. Maybe it is better to store flags into sharedPreferences and load the list only if necessary.

Comment posted by Amit pandey

you can use realm to store the data from server its essay

Comment posted by Xfce4

Thanks for the tip. Is there an open-source alternative?

Android Tags:android, back-button, kotlin

Post navigation

Previous Post: [FIXED] Is it possible to switch from Start state to End state in android MotionLayout using Java/Kotlin?
Next Post: [FIXED] android – PHP / SQL: cannot save image at server but successful save image link at SQL database

Related Posts

[FIXED] why is the “app” folder missing from my android studio 3.5.3 Android
[FIXED] How to create a dialog like Google Map in android Android
[FIXED] How to access and store list from firebase in android java Android
[FIXED] How many hits can google drive bare if i use it as cloud database? Android
[FIXED] android – How to create a customizable and reusable component? Android
[FIXED] iOS/Android – Disabling Mobile Data (Cellular Data) via SDK 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

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme