본문 바로가기
코리아 IT아카데미/android

5일차 | intent 개념 , 예제 1(버튼클릭->화면전환), 예제2(BMI 계산)

by Sharon kim 2022. 2. 18.

순서 암기

package com.example.myapp4;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    private String TAG1 = "Life_Cycle";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG1,"onCreate 호출됨");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG1,"onStart 호출됨");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG1,"onResume 호출됨");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG1,"onPause 호출됨");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG1,"onStop 호출됨");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG1,"onDestroy 호출됨");
    }
}
// 액티비티는 앱에 한 화면이다.
// portrait mode 가로모드는 랜드 스케이프 모드
//intent란 의도 또는 의사전달, 요청
// A라는 화면에 B라는 버튼 누르면 B화면 보여줌
// A 화면의 라이프사이클 / B 화면의 라이프 사이클
// 인텐트 
//1. 명시적 인텐트
//2. 암시적 인텐트

MainActivity2.java >> MainActivity 연습

 

//변수 : 태그 하나 더 만들기 (태그 필터 생성)

//Life cycle 메서드 오버라이드 해보기


button A 클릭했을 때 아래와 같이 나오고 intent B 화면으로 이동


intent B 화면 클릭했을 때 아래와 같이 나오고 button A 화면으로 이동

activity_intent_activity.xml

<?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=".IntentActivity_A">

    <Button
        android:id="@+id/ButtonA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button : A"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

IntentActivity_A.java

package com.example.myapp4;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

public class IntentActivity_A extends AppCompatActivity {

    final static String TAG = IntentActivity_A.class.getName();//IntentActivity_A

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

        Log.d(TAG,"A : onCreate 호출");

        Button button = findViewById(R.id.ButtonA);
//        button.setOnClickListener(v->{
//
//            // B라는 화면을 보내줘 -> os
//            // 1. 명시적 인텐트
//            Intent intent1 = new Intent(this, IntentActivity_B.class);
//            //1-1. 인텐트 값을 보내는 방법
//            intent1.putExtra("number1",1);
//            intent1.putExtra("roomNumber", 2);
//            intent1.putExtra("strData", "안녕");//키와 밸류값 맵 구조
//
//            //String 데이터 타입으로 바꾸기
//            startActivity(intent1);
//
//            //암시적 인텐트(사용자가 가지고 있는 자원 안에서 선택권을 줌 : naver를 어떤 브라우저로 열지)
////            Intent intent2 = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.naver.com"));
////            startActivity(intent2);
//        });
        button.setOnClickListener(v->{
            Intent intent = new Intent(this, IntentActivity_B.class);
            //값을 안보냄
            //startActivity(intent);
            startActivityForResult(intent, 10001);
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG,requestCode+"");
        Log.d(TAG,resultCode+"");
        Log.d(TAG,data+"");
        if(resultCode == RESULT_OK){
            int resultData = data.getIntExtra("result",0);
            Log.d(TAG,"resultData :"+ resultData);
        }

    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG,"A : onStart 호출");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG,"A : onResume 호출");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG,"A : onPause 호출");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG,"A : onStop 호출");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"A : onDestroy 호출");
    }
}

activity_intent_B.xml

<?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=".IntentActivity_B">

    <TextView
        android:id="@+id/textViewB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="INTENT B 화면입니다."
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

IntentActivity_B.java

package com.example.myapp4;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class IntentActivity_B extends AppCompatActivity {

    final static String TAG = IntentActivity_B.class.getName();//IntentActivity_B
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_b);

        if(getIntent() != null){
            int getNumber = getIntent().getIntExtra("number1", 0);
            int getRoomNumber = getIntent().getIntExtra("roomNumber", 0);
            String getStrData = getIntent().getStringExtra("strData");

            //String 데이터 타입으로 값 받기
            Log.d(TAG,"getNumber :" + getNumber);
            Log.d(TAG,"getRoomNumber :"+getRoomNumber);
            Log.d(TAG,"getStrData :"+getStrData);
        }
        TextView textView = findViewById(R.id.textViewB);
        textView.setOnClickListener(v->{
            Intent resultIntent = new Intent();
            resultIntent.putExtra("result",100);
//            if (true){
//                //값을 돌려 보낼 때 //()안에 커서 두고 ctrl p 하면 어떤 타입 넣어야하는지 알려줌
//                setResult(Activity.RESULT_OK, resultIntent);
//            }else{
//                setResult(Activity.RESULT_CANCELED, resultIntent);
//            }
            setResult(Activity.RESULT_OK, resultIntent);

            finish(); // 화면 종료 메서드
        });
        Log.d(TAG,"B : onCreate 호출");
    }



    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG,"B : onStart 호출");
    }

    @Override
    protected void onResume() {
        super.onResume();

        Log.d(TAG,"B : onResume 호출");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG,"B : onPause 호출");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG,"B : onStop 호출");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"B : onDestroy 호출");
    }
}

IntentActivity_C, D.java

-> A,B 복습


월요일에 계속 / 해보거나 같이 하거나

activity_bmi_input.xml(layout은 대문자 사용 안됨)

<?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=".BMI_InputActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.08"
        app:srcCompat="@drawable/winter_is_safe" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.55" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="신장"
        android:textSize="15dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@id/editText1"
        app:layout_constraintStart_toStartOf="@id/editText1" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.68" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="체중"
        android:textSize="15dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@id/editText2"
        app:layout_constraintStart_toStartOf="@id/editText2" />

    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="BMI지수 확인하기"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.83" />


</androidx.constraintlayout.widget.ConstraintLayout>