Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is static variable really safe to exchange data between activities?

I'm working on an application that consist of a couple of activities:

  • Activity 1: main screen of an application with a "Start" button
  • Activity 2: user selects its identity from the list (more than one user is going to use the application)
  • Activity 3: user inputs password
  • Activity 4: user chooses an event from a timetable (every user has its own timetable with associated events)
  • Activity 5: user can choose an action connected with an activity.
  • Activities 6-10: user performs appropriate action.

Below some more information:

  • every activity from 6-10 have to know what user is logged in and what event has been selected
  • every activity from 6-10 has a menu that allows the user to go back to activities: 1 (to log out), 4 (to select different event), 5 (to select different action)

Since now, I've been using bundles to exchange data between activities but it seems to complicate the code as the number of actions grow (some actions use a 3-4 activities to collect data from the user). Passing all the data to every created activity doesn't seems to be nice.

I'm thinking about storing the "user name" and selected "event" as a static fields of a class. I will simplify the code very much, but I'm not sure whether this data will persist if the user let say at some point press "home button" and run another application that needs a lot of memory.

Will the data stored in static fields be safe?

like image 454
kmalmur Avatar asked Dec 14 '11 11:12

kmalmur


People also ask

Are static variables safe?

Static variables are not thread safe. Instance variables do not require thread synchronization unless shared among threads. But, static variables are always shared by all the threads in the process. Hence, access to static variable is not thread safe.

Why static variable should not be used?

Static variables are generally considered bad because they represent global state and are therefore much more difficult to reason about. In particular, they break the assumptions of object-oriented programming.

Are static variables good practice?

Static Methods/Variables are bad practice. In short: Yes. There are many disadvantages and static methods should almost never be used. Static methods allow procedural/functional code to be shoe-horned into an Object Oriented world.

What is a benefit of static variable?

Benefits of static variables: constants can be defined without taking additional memory (one for each class) constants can be accessed without an instantiation of the class.


1 Answers

It's better to have a custom Application object and store them there. The application object will live aslong as your app does.

http://developer.android.com/reference/android/app/Application.html

You can get access to the Application object by casting getApplicationContext() to whatever your custom Application type is:

public class CustomApplication extends Application {
    private String userId;

    public void onCreate() {
        super.onCreate();
        ...
    }

    public String getUserId() {
        return userId;
    }

    ...
}

From Activity call: ((CustomApplication) getApplicationContext()).getUserId();

like image 182
Chris Banes Avatar answered Nov 14 '22 06:11

Chris Banes