Định nghĩa
Context là ngữ cảnh của trạng thái hiện tại của ứng dụng/đối tượng. Nó cho phép các đối tượng mới được tạo hiểu những gì đang diễn ra. Thông thường, bạn dùng Context để lấy thông tin về một phần khác của chương trình (Activity, Package/Application).
Trong ví dụ dưới đây, chúng ta tạo TextView động và truyền Context. Context này được dùng để lấy thông tin về môi trường.
Các bước thực hiện
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.
Bước 2: Layout (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout"
android:orientation="vertical"
android:padding="16sp"
tools:context=".MainActivity">
</LinearLayout>
Bước 3: Minh họa Application Context vs Activity Context (MainActivity.java)
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = findViewById(R.id.linearLayout);
// Application Context - sống suốt vòng đời app
TextView textViewContext = new TextView(getApplicationContext());
textViewContext.setTextSize(30);
textViewContext.setText("Application Context");
textViewContext.setTextColor(Color.RED);
// Activity Context - gắn với vòng đời Activity này
TextView textViewActivityContext = new TextView(this);
textViewActivityContext.setTextSize(30);
textViewActivityContext.setText("Activity Context");
linearLayout.addView(textViewContext);
linearLayout.addView(textViewActivityContext);
}
}
Bước 4: Manifest (AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Chạy ứng dụng
Kết nối thiết bị, nhấn Run. Giao diện hiển thị 2 dòng text thể hiện 2 loại Context.

Khi nào dùng loại nào?Tránh memory leak: Không giữ reference Activity Context trong object sống lâu (static field, singleton, thread dài).
- Application Context (getApplicationContext()): Singleton, sống suốt app. Dùng cho: Singleton, Database, SharedPreferences, Service, BroadcastReceiver đăng ký trong Manifest.
- Activity Context (this / activity): Gắn với vòng đời Activity. Dùng cho: UI (Dialog, Toast, View inflate), startActivity, layout inflation cần theme.