Is a static variable safe for exchanging data between actions?

I am working on an application that consists of several actions:

  • Step 1: the main screen of the application with the Start button
  • Step 2: the user selects his identity from the list (more than one user will use the application)
  • Step 3: input user password
  • Step 4: the user selects an event from the schedule (each user has his own schedule with the corresponding events)
  • Step 5: The user can select the action associated with the action.
  • Actions 6-10: The user performs the appropriate actions.

See below for more information:

  • every action from 6-10 should know that the user has registered and which event has been selected.
  • each action from 6-10 has a menu that allows the user to return to the actions: 1 (to exit the system), 4 (to select another event), 5 (to select another action)

Since then I use packages to exchange data between actions, but it seems to complicate the code as the number of actions increases (some actions use 3-4 actions to collect data from the user). Transferring all data to all created actions does not seem pleasant.

I am thinking about saving the "username" and the selected "event" as static fields of the class. I will greatly simplify the code, but I'm not sure if this data will be saved if the user at some point says, press the "home button" and start another application that needs a lot of memory.

Is data stored in static fields stored?

+6
source share
7 answers

Not. You are not guaranteed that they will all exist in the same class loader, in which case you will be dealing with different copies of these classes in different places and not gain access to the same shared state.

Now it can happen; but no, this is not what I would call "safe."

+2
source

It is better to have your own application object and store it there. The application object will live as good as your application.

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

You can access the Application object by inserting getApplicationContext () into your own application type:

public class CustomApplication extends Application { private String userId; public void onCreate() { super.onCreate(); ... } public String getUserId() { return userId; } ... } 

From the operation call: ((CustomApplication) getApplicationContext()).getUserId();

+5
source

you can use SharedPreference to do this instead of using a static variable / object in the class. check out this blog http://android-er.blogspot.com/2011/01/example-of-using-sharedpreferencesedito.html

+1
source

Static fields work, but not elegant. You just need the session object following the singleton pattern.

0
source

Static data will be saved when you press the home button, and when you open the application again, you will return to the same state that you left, but when you launch another application that requires a lot of memory, there are opportunities that can lose your static data. But if you have very little static data, it may persist.

You can also watch SharedPreference.

0
source

You should simply use the system of intent as designed. Statics and activity can be killed, apparently, perforce from Android. Even the Application class can be killed.

Say that you have an application that consists of two actions, the first of which allows the user to store some data in the Application object. Then the user presses the button to start the second action, which displays the data. The user removes his phone and returns to it a few hours later.

Android may decide to kill the application for various reasons during this time. When the user returns, picks up the phone and restarts your application, a new application object will be created and the second action will be restored, but the data entered by the user will no longer be available in the Application object, because it is a new application object.

One way to help with this is to use SharedPreferences even for complex objects. Gson is quite capable of serializing and deserializing them into SharedPreferences.

To simulate this, you can do the following:

$ adb shell ps | grep your.app.package

to get the pid of your running application, then

$ adb shell kill -9

then open the application using the task switcher and you will have a new application object, but it will be included in the second activity.

0
source

You can use static, unless you are sure that this static variable will not conflict with users. also a static variable is a property of the class, so it consumes more memory, because the lifetime is the application level. another, I can suggest using general privileges, which are very convenient. These general preferences may be accessible by any of the actions in your application. therefore, you do not need to pass the packet and worry about static fields. The last option is something that I don’t think is important in your application, "Use a database to store and return."

Hope this helps.

-1
source

Source: https://habr.com/ru/post/903759/


All Articles