ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [안드로이드 스튜디오] 양방향 액티비티
    프로그래밍/XML | JAVA 2024. 4. 8. 15:12

    양방향 액티비티와 데이터 전달

    • 메인 액티비티에서 세컨드 액티비티로 데이터를 넘긴 후에 다시 세컨드 액티비티에서 메인 액티비티로 데이터를 돌려주는 경우도 있음


    양방향 데이터 전달 예제

    • 메인 액티비티의 에디트텍스트의 두 수를 세컨드 액티비티에서 더한 후에 다시 메인 액티비티로 돌려줌

    Activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/edtNum1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:text=""
            tools:layout_editor_absoluteX="4dp"
            tools:layout_editor_absoluteY="23dp" />
    
        <EditText
            android:id="@+id/edtNum2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:text=""
            tools:layout_editor_absoluteX="10dp"
            tools:layout_editor_absoluteY="95dp" />
    
        <Button
            android:id="@+id/btnNewActivity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="더하기"
            tools:layout_editor_absoluteX="44dp"
            tools:layout_editor_absoluteY="172dp" />
    </LinearLayout>

    MainActivity.java

    package com.example.a10_17;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    public class MainActivity extends AppCompatActivity {
        EditText edtNum1;
        EditText edtNum2;
        Button btnNewActivity;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            btnNewActivity = (Button) findViewById(R.id.btnNewActivity);
            edtNum1 = (EditText) findViewById(R.id.edtNum1);
            edtNum2 = (EditText) findViewById(R.id.edtNum2);
            btnNewActivity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getApplicationContext(), secondActivity.class);
                    intent.putExtra("NUM1", Integer.parseInt(edtNum1.getText().toString()));
                    intent.putExtra("NUM2", Integer.parseInt(edtNum2.getText().toString()));
                    startActivityForResult(intent, 0);
                }
            });
        }
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
                int hap = data.getIntExtra("HAP", 0); // 이거 변수가 틀린거였음
                Toast.makeText(getApplicationContext(), "합계 : " + hap, Toast.LENGTH_SHORT).show();
            }
        }
    
    }

    second.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/btnReturn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="돌아가기"
            tools:layout_editor_absoluteX="129dp"
            tools:layout_editor_absoluteY="31dp" />
    </LinearLayout>

    secondActivtiy.java

    package com.example.a10_17;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    public class secondActivity extends AppCompatActivity {
        Button btnReturn;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second);
    
            Intent inIntent = getIntent();
            final int hapValue = inIntent.getIntExtra("NUM1", 0) + inIntent.getIntExtra("NUM2", 0);
    
            btnReturn = (Button) findViewById(R.id.btnReturn);
            btnReturn.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Intent outIntent = new Intent(getApplicationContext(), MainActivity.class);
                    outIntent.putExtra("HAP", hapValue);
                    setResult(RESULT_OK,outIntent);
                    finish();
                }
            });
    
        }
    
    }

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
    
        <application
            android:allowBackup="true"
            android:dataExtractionRules="@xml/data_extraction_rules"
            android:fullBackupContent="@xml/backup_rules"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme._10_17"
            tools:targetApi="31">
            <activity
                android:name=".MainActivity"
                android:exported="true">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".secondActivity" android:label="secondActivity">
            </activity>
        </application>
    
    </manifest>

     

Designed by Tistory.