Android
[Android] 안드로이드 Intent(인텐트) / 화면(액티비티) 전환 / 값 전달
jokey12
2020. 12. 30. 21:49
화면(액티비티) 전환
AndroidManifest.xml 내에 전환 될 액티비티(SecondActivity) 추가
// <application> 내에 추가
<activity android:name=".SecondActivity" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">
<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/Theme.Test">
<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" />
</application>
</manifest>
//MainActivity 코드
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
값 전달
intent로 값 전달은 putExtra( key, value ) 형식으로 전달한다.
//MainActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("숫자", 12345);
intent.putExtra("문자", "가나다라");
startActivity(intent);
Second_Activity에서는 getIntExtra, getStringExtra, getCharExtra, getBooleanExtra, getByteExtra 등 형식에 맞게
MainActivity에서 지정한 key값인 "숫자" , "문자" 에 맞는 값을 각각 받아올 수 있다.
//Second_Activity
TextView tv = (TextView) findViewById(R.id.tv);
Intent intent = getIntent();
int num = intent.getIntExtra("숫자", 0);
String message = intent.getStringExtra("문자");
tv.setText(num + message);