안드로이드 앱을 개발하다 보면 화면(액티비티) 사이에 데이터를 주고받아야 하는 경우가 매우 많습니다. 일반적으로는 Intent에 데이터를 담아 전달하는 방식이 표준이지만, 상황에 따라 Intent를 사용하지 않고 데이터를 공유해야 할 때도 있습니다.
이 글에서는 Intent의 부가 데이터(extra)를 사용하지 않고 두 가지 방법으로 액티비티 간 데이터를 전달하는 방법을 소개합니다.
- 정적(static) 변수와 Getter 메서드 활용
- SharedPreferences 활용
방법 1. 정적 변수와 Getter 메서드 사용하기
가장 간단한 방법은 첫 번째 액티비티에 static 변수와 이를 반환하는 public static Getter 메서드를 선언하고, 두 번째 액티비티에서 해당 메서드를 호출해 값을 읽어오는 것입니다.
1단계 — 새 프로젝트 생성
Android Studio에서 File → New Project를 선택한 뒤, 필요한 정보를 모두 입력하여 새 프로젝트를 생성합니다.
2단계 — res/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:layout_margin = "16dp"
android:orientation = "vertical"
tools:context = ".MainActivity">
<EditText
android:id = "@+id/edit_text"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_gravity = "center"
android:hint = "Enter something to pass"
android:inputType = "text" />
<Button
android:id = "@+id/button"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "center"
android:layout_marginTop = "16dp"
android:text = "Next" />
</LinearLayout>사용자가 값을 입력할 수 있는 EditText와 다음 화면으로 이동하는 Button으로 구성된 기본 레이아웃입니다.
3단계 — src/MainActivity.java 작성
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private static String value;
public static String getValue() {
return value;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = findViewById(R.id.edit_text);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
value = editText.getText().toString().trim();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}여기서 핵심은 버튼 클릭 시 입력값을 static 변수 value에 저장한다는 점입니다. Intent에는 어떤 데이터도 담지 않고 단순히 화면만 전환합니다.
4단계 — res/layout/activity_second.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:layout_margin = "16dp"
android:orientation = "vertical"
tools:context = ".SecondActivity">
<TextView
android:id = "@+id/text_view"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_gravity = "center" />
</LinearLayout>5단계 — src/SecondActivity.java 작성
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textView = findViewById(R.id.text_view);
textView.setText(MainActivity.getValue());
}
}두 번째 액티비티에서는 MainActivity.getValue()를 호출해 저장된 값을 가져온 뒤 TextView에 표시합니다.
6단계 — AndroidManifest.xml 등록
<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "https://schemas.android.com/apk/res/android"
package = "com.example.myapplication">
<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>
<activity android:name = ".SecondActivity"></activity>
</application>
</manifest>새로 만든 SecondActivity를 반드시 매니페스트에 등록해야 합니다. 등록하지 않으면 앱 실행 시 오류가 발생합니다.
실행 결과 확인
실제 안드로이드 기기를 컴퓨터에 연결한 상태에서 Android Studio 툴바의 Run ▶ 아이콘을 클릭해 앱을 실행합니다. 실행할 기기를 선택하면 다음과 같은 초기 화면이 표시됩니다.

값을 입력하고 Next 버튼을 누르면 두 번째 화면에서 입력했던 값이 그대로 출력되는 것을 확인할 수 있습니다.

방법 2. SharedPreferences 사용하기
정적 변수는 프로세스가 종료되면 값이 사라지고, 화면 회전 등 구성 변경 시에도 의도치 않게 초기화될 수 있습니다. 좀 더 안정적인 방법은 SharedPreferences를 사용해 데이터를 저장하고 다른 액티비티에서 읽어오는 것입니다.
1단계 — 새 프로젝트 생성
앞선 방법과 동일하게 Android Studio에서 File → New Project로 새 프로젝트를 생성합니다.
2단계 — res/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:layout_margin = "16dp"
android:orientation = "vertical"
tools:context = ".MainActivity">
<EditText
android:id = "@+id/edit_text"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_gravity = "center"
android:hint = "Enter something to pass"
android:inputType = "text" />
<Button
android:id = "@+id/button"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "center"
android:layout_marginTop = "16dp"
android:text = "Next" />
</LinearLayout>3단계 — src/MainActivity.java 작성
package com.example.myapplication;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = findViewById(R.id.edit_text);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String value = editText.getText().toString().trim();
SharedPreferences sharedPref = getSharedPreferences("myKey", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("value", value);
editor.apply();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}버튼 클릭 시 입력값을 getSharedPreferences("myKey", MODE_PRIVATE)로 얻은 에디터에 putString()으로 저장한 후 apply()로 반영합니다. 이후 Intent로 화면만 전환합니다.
4단계 — res/layout/activity_second.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:layout_margin = "16dp"
android:orientation = "vertical"
tools:context = ".SecondActivity">
<TextView
android:id = "@+id/text_view"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_gravity = "center" />
</LinearLayout>5단계 — src/SecondActivity.java 작성
package com.example.myapplication;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textView = findViewById(R.id.text_view);
SharedPreferences sharedPreferences = getSharedPreferences("myKey", MODE_PRIVATE);
String value = sharedPreferences.getString("value","");
textView.setText(value);
}
}같은 이름("myKey")의 SharedPreferences를 열고 getString("value", "")로 저장된 값을 읽어 TextView에 표시합니다. 두 번째 인자는 값이 존재하지 않을 때 반환될 기본값입니다.
6단계 — AndroidManifest.xml 등록
<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "https://schemas.android.com/apk/res/android"
package = "com.example.myapplication">
<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>
<activity android:name = ".SecondActivity"></activity>
</application>
</manifest>실행 결과 확인
기기를 연결한 뒤 Android Studio에서 Run ▶ 아이콘을 클릭해 앱을 실행합니다. 값을 입력하고 Next 버튼을 누르면 SharedPreferences에 저장된 값이 두 번째 화면에 정상적으로 표시됩니다.

마무리 및 참고 사항
이상으로 Intent의 extra를 사용하지 않고 액티비티 간 데이터를 전달하는 두 가지 방법을 살펴보았습니다.
- 정적 변수 방식: 구현이 가장 간단하지만, 앱 프로세스가 종료되거나 메모리가 부족해 액티비티가 재생성되면 값이 유실될 수 있습니다.
- SharedPreferences 방식: 데이터가 디스크에 저장되므로 프로세스가 종료되어도 값이 유지됩니다. 다만 문자열·숫자 같은 단순 데이터에 적합하며, 대용량 객체 전달에는 적합하지 않습니다.
실무에서는 일반적으로 ViewModel, 싱글턴 패턴, 또는 Intent/Bundle을 조합해 사용하는 것이 권장되지만, 간단한 값 전달이나 설정값 공유에는 위 두 방법 모두 유용하게 활용할 수 있습니다.