0

I'm a just diving head first into Android Studio and am working on my first app which is a sort of surfing log application. Basically it is to keep track of various weather conditions each time the user goes out surfing.

I have created a Java class "Session" with the following fields:

  • Date (Date)
  • Location (String)
  • Tide height (float)
  • Surf size (float)
  • more to come, keeping it simple for now.

My application will flow as follows:

There will be a Main home screen activity, with buttons, and also a small tableview that displays your 3 most recent sessions. Buttons including:

  • New Session: this takes you to a new activity with various text inputs for each of the above fields, a date selector... and a save button.
  • My Sessions: this will take you to a tableview where you can view all of your past sessions. You can organize them by location, date, surf size...

Thus my question:

What is the best practice to pass all this data between the various activities?

To me the most obvious way to go about this is to have a central ArrayList that gets loaded in the main activity and then this gets somehow passed around to all of the subsequent activities. It contains all the sessions that you have ever created.

So lets say I tap the "new session" button on the main screen. This takes me to the new Session activity. I enter all the fields and hit save. I would like to take this data, create a new session object and then add this to the array back in Main Activity. So far my research tells me to pass all this data back using a bundle and the intent.putextra() technique. However it seems cumbersome. Is there not a better approach where I could just create a new object of my Session class and then append it to the central array?

Again let's say I tap the 'my sessions' button from the main activity. I would like this to load up a new activity which is a tableview that allows the user to scroll through all of their previously created sessions and tap on one to view the details.

I've done a bit of research and it seems there are various ways of going about this, I've read up a bit on Singletons, I've looked into creating some sort of static class that I could then reference from multiple activities, I've read about parcelable and serializable...

Perhaps though someone with some android experience could shed some light on the most efficient and standard way of accomplishing what I would like to do.

Again I want to reiterate that this Array or collection of "Sessions" is going to be the center of the app. Pretty much every activity I implement down the road is going to be using this data in someway or another. Whether it's displaying it a tableview which can be sorted in different ways, to running statistical analysis on it, to displaying pins on a map of each location...

  • FYI, the troublesome old date-time classes such as `java.util.Date` are now legacy, supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. Much of the java.time functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Jan 31 '18 at 22:28
  • Good to know thanks. I had noticed they were deprecated and was looking into other options. I'll look into time. – Ryan Kirkpatrick Jan 31 '18 at 23:25

2 Answers2

1

I think you want to keep your data in a database and use a pub/sub workflow to keep track of it.

For example, you can use Firebase. Everytime a part of your application does an update (even a different user on a different device) any other part of your code can listen to that change and capture it.

Firebase is just an example, RethinkDB, MongoDB all have this option.

It is not a good option to use a data structure that is on your app: it will become really messy if it needs to be shared between different parts of your app, and impossible if you need other users to be aware of the change.

Gratus D.
  • 787
  • 7
  • 22
  • 1
    Or even plain built in SQLite if you don't want to share data. But some sort of database, definitely. – Gabe Sechan Jan 31 '18 at 22:11
  • Each users accumulation of 'sessions' are going to be personal. In other words, your log is your log and only your log. I'm not trying to create a collective pool. Each user will have their own personal set of data. – Ryan Kirkpatrick Feb 01 '18 at 18:59
  • Then the client side SQLite approach @GabeSechan mentioned might be best. You might want to consider https://github.com/google/agera – Gratus D. Feb 01 '18 at 19:24
0

I think you should go for an event-based library like RxJava2. you can easily subscribe to a bus (subject) which emits all data created up till now.to keep other app's components up-to-date.

for you specefic use case. there is sth great in RXjava2 is called Replay Subject

Replay Subject: It emits all the items of the source Observable, regardless of when the subscriber subscribes

a simple implementation would look like

 public class RxReplayBus {


private static ReplaySubject<Object> subject = ReplaySubject.create();

private RxReplayBus() {
    // hidden constructor
}


public static Disposable subscribe(@NonNull Consumer<Object> action) {
    return subject.subscribe(action);
}

public static void publish(@NonNull Object message) {
    subject.onNext(message);
  }

}

from your component subscribe :

  disposable = RxReplayBus.subscribe(new Consumer<Object>() {
        @Override
        public void accept(Object o) throws Exception {


        }
    });

and unsubscribe using :

    disposable.dispose();
Bishoy Kamel
  • 2,327
  • 2
  • 17
  • 29