0

I've an issue with passing a variable back to the first activity.

My app starts with one activity, which opens a second one, which opens an third one, which opens a forth, which opens the first activity again. Now I want to get a variable, which I get from a user input in the third activity in my first one. I already managed to pass variables between two activities there and back with onActivityResult() but I do not get how to manage this between more than two activities.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    Probably bad practise, but sometimes it's less hassle to create a singleton global object to store variables. – grolschie Aug 26 '20 at 23:40
  • This seems like something trivial if you were using anything other than multiple activities. Is there a reason you're using multiple activities? – ianhanniballake Aug 27 '20 at 00:25
  • 1
    Use sharedPrederenecs and you will be able to use the same variable anywhere in your app. https://stackoverflow.com/q/23024831/13685592 – KalanaChinthaka Aug 27 '20 at 00:39
  • since I only want to access one variable I guess sharedPreferences will fill my needs perfectly! I have to use several activities because every activity is a filling form with about 30 user inputs each, which leads to the next filling form. The first activity is the main menu, where I need that variable from activity2. – Hadadadebadada Aug 27 '20 at 01:08

2 Answers2

2

use bundle

you can use Bundle for move the value from first activity to second activity

check this link ---> [here] (Passing a Bundle on startActivity()?)

if use value in several activity you can use SharePrefrence or you can make class extends Application and make value in the class and use the values in several activity

be careful if close the app destroy the values

0

You can use shared preferences to access variables in all your activities or use can use this method:

When going from fourth activity to first use startActivity(intent) and add the variable as an extra in intent. And in first activity override onBackPressed. This may not be good practice but it works.

ASHIR
  • 58
  • 1
  • 8