c# – What can the use of static variables lead to?


Good evening everyone.
In the process of developing an application, there is a great need to transfer any variables from one Activity to another. I have always used the standard method:

Intent intent = new Intent(First.this, Second.class);

And then getting:

String from_to = getIntent().getExtras().getString("key", "null");

But I am frankly sick of this method, and I decided to do it through static variables like this:

static String from_to;


from_to = "Hello dear Android";

And in another activity:

String from_to = First.from_to;

But now I am tormented by the question: what could this turn out to be for me ?!

Thanks in advance to everyone!


will result in a NullPointerException at that wonderful moment when the application is completely unloaded from memory and will be opened again (for example, minimized by the Home button and opened from the list of last launched ones).

Let's assume the following situation: Activity_A has a static field, Activity_B uses it. the current stack is such Activity_A -> Activity_B. the application was unloaded from memory, all references to objects, including static fields, were zeroed out. The application is restored from memory, Activity_B will be loaded first (since it is at the top of the stack), which accesses a static field in Activity_A , and immediately gets a NullPointerException . I hope I described it clearly.

just the same method of transmission via Intent is more preferable, since all passed parameters will be saved together with the state of the current activity and will be restored in the same way.

Scroll to Top