Android

Android] Intent를 이용하여 데이터 넘기기

김코식 2022. 2. 14. 16:34

Intent를 이용한 액티비티간 데이터 전달

1) 텍스트 전달

SubActivity를 사용함으로 manifest에 다음 추가

<activity android:name=".SubActivity"/>

 

1. activity_main.xml

   ㄴ EditText 추가

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="데이터 넘기기"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:id="@+id/edt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="넘기고 싶은 데이터를 입력하십시오."
        app:layout_constraintStart_toStartOf="@+id/btn1"
        app:layout_constraintEnd_toEndOf="@+id/btn1"
        app:layout_constraintBottom_toTopOf="@+id/btn1"/>


</androidx.constraintlayout.widget.ConstraintLayout>

 

2. MainActivity.java

   ㄴ 데이터를 넘기기위해 putExtra(key,data)사용 

package com.example.intentdata;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    EditText editText;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.edt1);
        button = findViewById(R.id.btn1);
        button.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                String input = editText.getText().toString();
                Intent intent = new Intent(MainActivity.this,SubActivity.class);
                intent.putExtra("data",input);
                startActivity(intent);
            }
        });

    }

}

 

 

String input = editText.getText().toString();

ㄴ>> edittext에서 입력받은 데이터값을 input 에 저장

Intent intent = new Intent(MainActivity.this,SubActivity.class);
intent.putExtra("data",input);
startActivity(intent);

ㄴ>> 인텐트를 사용하여 input에 저장되어 있는 데이터를 "data"키를 통해 SubActivity.class에 전달하고 액티비티 실행

 

2.SubActivity.java

package com.example.intentdata;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class SubActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);

        Intent intent = getIntent();
        String string = intent.getStringExtra("data");

        TextView textView = findViewById(R.id.tex1);
        textView.setText(string);
    }
}
Intent intent = getIntent();
String string = intent.getStringExtra("data");

ㄴ>> 인텐트를 이용하여 string에 key가 "data"인 데이터 전달 받는다

TextView textView = findViewById(R.id.tex1);
textView.setText(string);

ㄴ>> textView에 string안에 저장된 데이터 불러온다

 

완료