Search code examples
androidandroid-activity

Send data between activities while skipping one middle activity


I have a simple login app with three activities.

ActivityA is the launcher activity and the login screen. ActivityB has two buttons, "Configure" and "Exit". And ActivityC is a screen for setting a new username and password.

I also have a class called User.

I need to send data from ActivityA to ActivityC. That data is an User object with the default values for password and username.

Here are the steps of what I need to do, in order:

1 - Launch the app with ActivityA and click a button ("Login") that sends me to ActivityB.

2 - Then, in ActivityB I need to click another button ("Configure") that should send me to ActivityC.

3 - From ActivityC, I set a new password and username and click a button ("Confirm") that should send me back to ActivityA. In ActivityA, I need data from ActivityC. That data is an User object with the new values for password and username.

Here are the screens for Activities A, B and C, so it's easier to understand.

Here's ActivityA:

enter image description here

Here's ActivityB:

enter image description here

And here's ActivityC:

enter image description here

So far I've accomplished steps 1 and 2. But I don't know how I can send the data from ActivityA to ActivityC without launching ActivityC because when clicking the button in ActivityA, I'll call ActivityB instead. But the data still needs to be available to ActivityC.

I'm currently passing data from ActivityA to ActivityB and then to ActivityC. That works. But I'd like to know if that's unnecessary and if there's a better way. Because while i only currently have 3 activities, if I had more it would become complicated to keep on chaining the activities like that, right? So far I only know how to pass data with Intent.

I'd also like to pass the full object, if possible. As opposed to just passing the fields of said object (String, int, etc).

If there's way, how can I achieve that? Thank you.


Solution

  • Use shared preferences for that. https://developer.android.com/training/data-storage/shared-preferences