상세 컨텐츠

본문 제목

(안드로이드 미션) 액티비티간 통신을 해보자~! (startActivityForResult 활용예제)

Android

by choiDev 2019. 1. 29. 15:07

본문

액티비티간 통신을 해보자~!


안녕하세요 초이 입니다~

오늘은 액티비티간 Intent를 사용해서 데이터를 주고 받는 문제를 풀어보려고 합니다~

바로 문제부터 드리겠습니다~


[문제 내용]

1. 로그인 액티비티와, 메뉴 액티비티를 각각 만듭니다.

2. 로그인 액티비티에는 버튼이 1개

3. 메뉴 액티비티에는 버튼이 3개

4. 로그인 화면의 버튼을 누르면 메뉴 화면으로 이동합니다.

5. 메뉴 화면의 버튼 중에서 하나를 누르면 로그인 화면으로 돌아온 후 선택된 메뉴의 이름을 토스트 메시지로 보여줍니다.


[로그인 액티비티]


[메뉴 액티비티]


[결과화면]








네~ 여기까지가 문제이고 정답은 바로 아래 남겨 둘테니 정답을 참고하세요~




정답코너


[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"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="7"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="로그인 하기"/>
</LinearLayout>

<LinearLayout
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/login"
android:text="로그인"/>
</LinearLayout>

</LinearLayout>


[activity_menu.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">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="메인 메뉴"/>
</LinearLayout>

<LinearLayout
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/customer"
android:layout_marginBottom="15dp"
android:text="고객 관리"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sales"
android:layout_marginBottom="15dp"
android:text="매출 관리"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item"
android:text="상품 관리"/>
</LinearLayout>


</LinearLayout>


[MainActivity.java]

package com.example.junho.myapplication;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private static final int MENU_REQUEST_CODE = 1001;
Button button;

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

button = (Button) findViewById(R.id.login);

button.setOnClickListener(v -> {
Intent menuIntent = new Intent(MainActivity.this,MenuActivity.class);
startActivityForResult(menuIntent,MENU_REQUEST_CODE);
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(resultCode == RESULT_OK){
MenuType type = (MenuType) data.getExtras().getSerializable("result");

Toast.makeText(this, type.toString(), Toast.LENGTH_SHORT).show();
}
}
}


[MenuActivity.java]

package com.example.junho.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;

public class MenuActivity extends AppCompatActivity {
Button cusButton;
Button salesButton;
Button itemButton;

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

cusButton = (Button) findViewById(R.id.customer);
salesButton = (Button) findViewById(R.id.sales);
itemButton = (Button) findViewById(R.id.item);

cusButton.setOnClickListener(v ->
finishActivity(MenuType.CUSTOMER_RESULT));
salesButton.setOnClickListener(v ->
finishActivity(MenuType.SALES_RESULT));
itemButton.setOnClickListener(v ->
finishActivity(MenuType.ITEM_RESULT));
}

public void finishActivity(MenuType resultCode){
Intent intent = new Intent();
intent.putExtra("result",resultCode);
setResult(RESULT_OK,intent);
finish();
}
}


[MenuType.java]

package com.example.junho.myapplication;

import java.io.Serializable;

public enum MenuType implements Serializable {
/**
* CUSTOMER_RESULT = MenuActivity에서 고객관리 버튼을 눌렀을 때 반환되는 값
*
* SALES_RESULT = MenuActivity에서 매출관리 버튼을 눌렀을 때 반환되는 값
*
* ITEM_RESULT = MenuActivity에서 상품관리 버튼을 눌렀을 때 반환되는 값
*/
CUSTOMER_RESULT ,SALES_RESULT, ITEM_RESULT;
}


관련글 더보기