Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+10 votes

Hey guys so in each activity where the user inputs a date or amount of time the person lived for I have get methods to retrieve that data. I am now try to access that data in another activity where I will do the calculations. I looked up a tutorial and it said to use,

Date birthDate = "OriginalActivityName".getBirthDate();

but this doesn't seem to be working because the activities are static. Any suggestions on a better way to do this?

asked in CSC490_Spring2019 by (8 points)
0

I know the thing called Intent in the API should handle the cross activity communication problem

4 Answers

+4 votes

Are you using two separate activities? Activities are responsible for controlling user interfaces/different applications views/screens. If you just want to do calculations with the date elsewhere, you could create a separate DateManager class with public methods for setting birth date/death date and retrieving age, ect.

answered by (8 points)
+4 votes

I'm doubtful that using separate activities is the best way to go with this -- one activity should be plenty, and it's easier to keep all the data contained that way!

But... if you want to try to get it to work using that two-activity setup:

a) You could declare a static field inside of the first activity, and set that. Then you would be able to access it statically from the second activity. (Not the best coding style though...)

b) You can use "extras" attached to Intents to pass information between activities. But you might need to pass strings/integers, rather than a date type... not sure. See: https://zocada.com/using-intents-extras-pass-data-activities-android-beginners-guide/

answered by (449 points)
+4 votes

You can pass variables from one activity to another through the intent like so:

intent.putExtra("key", value);

Then you can call it in the next activity buy using this:

intent.getExtra("key", value);

There are different methods for different data types, so keep that in mind.

answered by (8 points)
edited by
+1 vote

Add to it maybe in work, team leader won't let you create a second activity it may damage user experience

answered by (8 points)
...