IT/Android

android - 양방향 intent

노마드오브 2018. 9. 24. 20:30

파일명 : activity_main.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="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<EditText
android:id="@+id/edtNum1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edtNum2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnNewActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="더하기"/>

</LinearLayout>


파일명 : activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:id="@+id/btnReturn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="돌아가기"/>

</LinearLayout>



파일명 : MainActivity.java

package com.example.it.myapplication8_5;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("메인 액티비티");

Button btnNewActivity = (Button) findViewById(R.id.btnNewActivity);
btnNewActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText edtNum1 = (EditText) findViewById(R.id.edtNum1);
EditText edtNum2 = (EditText) findViewById(R.id.edtNum2);

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) {
if (resultCode == RESULT_OK) {
int hap = data.getIntExtra("Hap", 0);
Toast.makeText(getApplicationContext(), "합계 : " + hap, Toast.LENGTH_SHORT).show();
}
}

}


파일명 : SecondActivity.java

package com.example.it.myapplication8_5;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
setTitle("Second 액티비티");

Intent inIntent = getIntent();
final int hapValue = inIntent.getIntExtra("Num1", 0) + inIntent.getIntExtra("Num2", 0);

Button btnReturn = (Button) findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new View.OnClickListener() {
@Override
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"
package="com.example.it.myapplication8_5">

<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>