Skip to content Skip to sidebar Skip to footer

Android Base Activity: Base's Global Variables, Can't Get From Some Activites

I'm taking an android class now, so I am somewhat new to android app development. My first assumption for a Base Activity is that it's Global Variables and it's values would be ava

Solution 1:

Global variables need to be declared static. Then they would be accessible from any class. Example:

publicclassGlobals{
    publicstaticString myString;
}

Any class can read/write the myString like this:

Globals.myString = "foo";

or

Stringbar= Globals.myString;

Solution 2:

From experience I believe the variables of one activity is only avaliable to the other while the activity is active, which means between onCreate and onDestroy, other then that you will probably get a null pointer exception, what you really should be doing is sending the data, or arrays, along with the intent to the other activity.

I dont think you should be calling on other activities variables, although it is possible as stated above. I believe when the activity has had it's onDestroy method called the objects in the activity are destroyed to and are removed from memory. Destroying anything that they held.

What is this base activity? Does it just extend activity? And then MainActivity is extending Activity as well? Only one activity is usable at any one time, if your doing what I think your doing you should have a service which can provide you with everything over the cycle of the application, just remember to stop it when your done with it.

Post a Comment for "Android Base Activity: Base's Global Variables, Can't Get From Some Activites"