본문 바로가기
App/Android Java

Android_Java(16) - MyActivity

by SeleniumBindingProtein 2022. 2. 10.
728x90
반응형

1. 4가지 개념
    1) 애플리케이션 (application)
        - 한 개 이상의 액티비티들로 구성됨 
            - 메인 액티비티(MainActivity)는 자동으로 등록됨
            - 추가한 세컨드 액티비티는 별도로 등록해줘야 함 
        - 액티비티들은 애플리케이션 안에서 느슨하게 묶여 있음 
    2) 액티비티 (activity)
        - 애플리케이션을 구성하는 빌딩 블록 
    3) 태스크 (task)
        - 스택에 있는 액티비티들 
        - 하나의 태스크는 스택에 있는 액티비티들로 구성됨 
    4) 액티비티 스택(activity stack)
        - Back 키를 누르면 현재 액티비티를 제거하고 이전 액티비티로 되돌아 감 
        - 사용자가 방문한 액티비들은 어딘가에 기억 
        - 액티비티1 : 태스크의 루트 액티비티 
          액티비티2
          액티비티3 (Back key) -> 액티비티3은 제거(파괴)됨
          액티비티1 

 

2. 액티비티 개요
    1) 액티비티 추가 
        - 일반적으로 액티비티 하나당 XML 파일 하나를 만들어서 사용
        - MainActivity.java 코드는 Activity 클래스를 상속 받음 


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="kr.co.myactivity">

    <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/Theme.MyActivity">
        <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="Second 액티비티"/>
    </application>
</manifest>

<MainActivity>

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

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnNewActivity"
        android:text="새 화면 열기"/>

</LinearLayout>
package kr.co.myactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        Button btnNewActivity = findViewById(R.id.btnNewActivity);
        btnNewActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 메인 액티비티에서 세컨드 액티비티를 호출
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);

            }
        });
    }
}

<SecondActivity>

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

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

</LinearLayout>
package kr.co.myactivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;

public class SecondActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.second);
        setTitle("Second 액티비티");

        Button btnReturn = findViewById(R.id.btnReturn);
        btnReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}

[결과 출력]

- '새 화면 열기' 클릭 시, SecondActivity 화면으로 이동

- '돌아가기' 클릭 시, MainActivity 화면으로 이동

01

728x90
반응형

'App > Android Java' 카테고리의 다른 글

Android_Java(18) - MyIntent  (0) 2022.02.10
Android_Java(17) - MyActivity(2)  (0) 2022.02.10
Android_Java(15) - FileExternalApp  (0) 2022.02.10
Android_Java(14) - FileApp  (0) 2022.02.09
Android_java(13) - AlertDialogApp  (0) 2022.02.09

댓글