?Sending a String created inside a method to another class
I have code which has one activity which passes information to a second activity. I can use this information to pass to a third activity with additional information from the result of the second activity.
I want to use gestures as a method of goin开发者_运维百科g back to a previous activity, but if I go back from the third to the second activity I need the information initially passed from the first to the second activity to still be present.
i.e.
First Acticity
what is Y?
answer y = 5
Second activity
User said Y = 5
what is X?
Third Activity
User said Y = 5 X = 6
Go back to Second activity but maintain the input of
User said Y = 5.
To do this I have used a bundle to pass the information between activities, but I can only access the info in the bundle from within a method within the class started by the intent.
the gesture controls are within another class, so I cannot access the bundle information from within this class as the getIntent command produces a not defined error.
What I need to do is to be able to pass the information from the bundle from the first activity to the gesture class so that I can pass it back when I go back using the gestures.
So you are saying that you need to be able to recover the previously input data when you go back?
There are a couple options:
1: Instead of bundling the data with the intent, you can store the data in the application object (which you can obtain via getApplication()
). This will make that data accessible from anywhere in your app.
2: Use SharedPreferences (as described here). You simply map the value you want to store with a string and it is likewise accessible anywhere in your app
3: Store the value in a static field in whatever class is persistent throughout the app's lifetime. This is the easiest to execute, but not the best style (i suggest using this as a last resort only)
Good Luck! Let me know if this was helpful.
精彩评论