시작 액티비티에서 서브 액티비티로 이동 후,
서브 액티비티에서 입력한 내용을 시작 액티비티로 옮기는 예제
1. 메인 액티비티 레이아웃
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.progdv.startactivityforresult.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25sp"
android:text="서브 액티비티 호출"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="리턴받은 문자열(아래쪽)" />
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="25sp"
android:textColor="#ff0000"
android:text="이곳에 출력하세요"
/>
</LinearLayout>
</RelativeLayout>
2. 서브 액티비티인 result activity 생성 후 레이아웃 설정
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.progdv.startactivityforresult.ResultActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="이곳에 리턴할 문자를 입력하세요."
/>
<LinearLayout
android:id="@+id/linearLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<Button
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="입력 완료"
android:layout_weight="1"
/>
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="취소"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
3. 메인 액티비티 클래스
package com.progdv.startactivityforresult;
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.TextView;
public class MainActivity extends AppCompatActivity {
Button btn;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.btn1);
result = (TextView)findViewById(R.id.result);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent =
new Intent(MainActivity.this, ResultActivity.class);
//startActivity(intent);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1) {
if(resultCode == RESULT_OK) {
String str = data.getStringExtra("INPUT_TEXT");
result.setText(str);
}else if(resultCode == RESULT_CANCELED) {
// 취소 버튼 클릭
}
}
}
}
4. 서브 액티비티 ResultActivity 클래스
package com.progdv.startactivityforresult;
import android.app.Activity;
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 ResultActivity extends AppCompatActivity {
EditText edit;
Button btn_ok, btn_cancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
edit = (EditText)findViewById(R.id.edit);
btn_ok = (Button)findViewById(R.id.btn_ok);
btn_cancel = (Button)findViewById(R.id.btn_cancel);
btn_ok.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("INPUT_TEXT", edit.getText().toString());
setResult(Activity.RESULT_OK, intent);
finish();
}
});
}
}
서브에서 입력한 값이 메인으로 잘 넘어온다.
'안드로이드' 카테고리의 다른 글
안드로이드 데이터베이스 SQLite (0) | 2016.05.02 |
---|---|
암시적 인텐트 (implicit intent) (0) | 2016.04.29 |
명시적 인텐트 (explicit intent) (0) | 2016.04.29 |
대화상자 (Dialog): AlertDialog (0) | 2016.04.28 |
컨텍스트 메뉴 (contextmenu) (0) | 2016.04.28 |