SoFunction
Updated on 2025-04-09

Detailed explanation of context and global variable instances in Android programming

This article describes the usage of context and global variables in Android programming. Share it for your reference, as follows:

When studying context today, I have a certain understanding of application and activity context. The following is the information copied from the Internet.

The difference between Application context and Activity context:

These are two different contexts, and the two most common ones. In the first type, the life cycle of the context is related to the life cycle of the Application. The context is destroyed with the destruction of the Application, and the life cycle of the application has nothing to do with the life cycle of the activity. The context in the second type is related to the life cycle of the Activity, but for an Application, the Activity can be destroyed several times, and the context belonging to the Activity will be destroyed many times. As for which context to use, it depends on the application scenario. I personally feel that using the context of Activity is better, but sometimes you must use the context of Application. The application context can be obtained through or through methods.

Also, when using context, be careful of memory leakage to prevent memory leakage, and pay attention to several aspects:

1. Do not let objects with long life cycle refer to the activity context, that is, ensure that the object that references the activity must be the same as the activity itself.

2. For objects with long life cycles, you can use application context

3. Avoid non-static internal classes, try to use static classes, avoid life cycle problems, and pay attention to life cycle changes caused by internal classes' reference to external objects.

Now let’s go back to the topic, let’s talk about Android global variables. During the development process, sometimes some global data may be needed to allow all the activities and Views in the application to be accessible. When you encounter this situation, you may first think of defining a class yourself and then creating many static members. Android has provided us with a solution to this situation:

In Android, there is an Application class. In Activity, you can use the getApplication() method to obtain instances. You can use it to obtain the current application's theme, content in the resource file, etc. One of the more flexible features of this class is that it can be inherited to add its own global attributes. For example, if you need to save scores to develop a game, then we can inherit Application. The following is a demo for your reference

First, write a subclass of Application:

import ;
public class GameApplication extends Application {
 private int score;
 public int getScore() {
  return score;
 }
 public void setScore(int score) {
   = score;
 }
}

Then modify it in the file:

<application android:name=".GameApplication" android:icon="@drawable/icon" android:label="@string/app_name">
  <activity android:name=".DemoActivity"
     android:label="@string/app_name">
   <intent-filter>
    <action android:name="" />
    <category android:name="" />
   </intent-filter>
  </activity>
  <activity android:name="ResultActivity"></activity>
</application>

Notice that android:name=".GameApplication" is added.

After the modification is completed, read:

public class DemoActivity extends Activity {
 public Button button;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView();
  button=(Button)findViewById();
  ((GameApplication)getApplication()).setScore(100);
  (new () {
   public void onClick(View v) {
    Intent intent=new Intent();
    (, );
    startActivity(intent);
   }
  });
 }
}

Set scores in this activity, we can take them out in other activities:

public class ResultActivity extends Activity {
  @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  (savedInstanceState);
  setContentView();
  TextView tv=(TextView)findViewById();
  int score=((GameApplication)getApplicationContext()).getScore();
  ("Your grades are:"+score);
 }
}

This is just a simple example. Of course, if you want to complete the above functions, just use intent to pass values, which is also troublesome. However, if you have many activities, you will find it useful to use this method. Can you also complete similar functions with sharepreference? Yes, but the efficiency is much worse than this. Sharepreference is mainly used to store data. When you exit the program, you can save the simple data you need to save into sharepreference. Of course, for complex data, you also have to use SQLite.

I hope this article will be helpful to everyone's Android programming design.