[안드로이드스튜디오] 명시적 인텐트 - 두개의 액티비티로 이루어진 애플리케이션

2021. 9. 11. 17:43·Programming/Mobile

'이미지 표시 액티비티 열기' 버튼 클릭 시 액티비티2가 시작

-> 액티비티1이 잠시 중단되고 액티비티2가 새로 시작된 것

-> 액티비티 스택에서 액티비티1 위에 액티비티2가 있을 것

'닫기'버튼 클릭 시 액티비티2가 종료

-> 액티비티 스택에 있는 액티비티1이 다시 실행됨

-> Back키를 눌러도 같은 결과


- layout1.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="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="여기는 액티비티1입니다."/>

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="이미지 표시 액티비티 열기"/>
</LinearLayout>

 

- layout2.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="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="여기는 액티비티2입니다."/>

    <ImageButton
        android:id="@+id/imageView1"
        android:layout_width="135dp"
        android:layout_height="248dp"
        android:src="@drawable/android"/>

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="닫기"/>
</LinearLayout>

 

- Activity1.java

package kr.co.company.ecplicitintent;

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

import androidx.appcompat.app.AppCompatActivity;

import kr.co.company.ecplicitintent.R;

public class Activity1 extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);
        Button b = (Button)findViewById(R.id.Button01);
        b.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                //버튼이 클릭되면 Activity2를 시작
                Intent intent = new Intent(Activity1.this, kr.co.company.ecplicitintent.Activity2.class);
                // 두번째 액티비티를 시작하려면 먼저 인텐트 객체를 생성
                // 두번째 액티비티의 이름을 알고 있으므로 두번째 액티비티의 클래스 이름을 인수로 주어서 인테트 객체 생성 -> 명시적인 인텐트 사용
                startActivity(intent);
            }
        });

    }
}

 

- Activity2.java

package kr.co.company.ecplicitintent;

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

import androidx.appcompat.app.AppCompatActivity;

import kr.co.company.ecplicitintent.R;

public class Activity2 extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout2);
        Button b = (Button)findViewById(R.id.Button01);
        b.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                finish();
                // 이벤트리스너에서는 버튼이 클릭되면 finish() 메소드를 호출하여서 현재의 액티비티를 종료
            }
        });

    }
}

 

- AndroidManifest.xml

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

    <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.EcplicitIntent">
        <activity
            android:name="kr.co.company.ecplicitintent.Activity1"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Activity2" android:label="Activity2"></activity>
        <!-- 매니페스트 파일 수정 : 액티비티는 매니페스트 파일에 등록되어야 함.
            android.name : 액티비티의 클래스 이름
            같은 패키지 -> .클래스이름 or 클래스이름
            다른 패키지 -> 패키지 이름 포함한 완전경로
            android.label : 타이틀바에 나타나는 텍스트 -->
    </application>

</manifest>

 

'Programming > Mobile' 카테고리의 다른 글

[안드로이드스튜디오] 서비스와 방송수신자  (0) 2021.10.03
[안드로이드스튜디오] 인텐트  (0) 2021.09.19
[안드로이드스튜디오] 액티비티로부터 결과받기  (0) 2021.09.12
[안드로이드스튜디오] 여러페이지로 구성된 애플리케이션 작성  (0) 2021.09.12
[안드로이드스튜디오] 액티비티  (0) 2021.09.11
'Programming/Mobile' 카테고리의 다른 글
  • [안드로이드스튜디오] 인텐트
  • [안드로이드스튜디오] 액티비티로부터 결과받기
  • [안드로이드스튜디오] 여러페이지로 구성된 애플리케이션 작성
  • [안드로이드스튜디오] 액티비티
min_sol
min_sol
  • min_sol
    비글개발연구소🐾
    min_sol
  • 전체
    오늘
    어제
    • 분류 전체보기 (281)
      • Programming (128)
        • Algorithm (52)
        • JAVA (40)
        • GIS (5)
        • PyQt (10)
        • C# (11)
        • Mobile (6)
        • AI (4)
      • Backend (38)
        • Spring (16)
        • JSP (11)
        • Network (5)
      • Frontend (29)
        • React (11)
        • Vue (13)
        • Next.js (4)
      • Database (10)
        • PostgreSQL (1)
        • Oracle (8)
        • Elasticsearch (1)
      • DevOps (8)
        • Linux (7)
        • Mac (1)
      • Tools (32)
        • IntelliJ (1)
        • VSCode (1)
        • GitHub (10)
        • RPA (20)
      • Security (9)
      • etc (22)
        • ERROR (5)
        • 세미나 | 교육 (11)
        • 자격증 (1)
        • 일상 (2)
        • 2021 (2)
  • 인기 글

  • 태그

    백준
    PyQt5
    spring
    자료구조
    연습문제
    이클립스
    vue.js
    자바
    명품자바에센셜
    알고리즘
    스윙
    jsp
    VUE
    코딩테스트
    Java
    RPA
    계산기
    생능출판
    PyQt
    자동화
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
min_sol
[안드로이드스튜디오] 명시적 인텐트 - 두개의 액티비티로 이루어진 애플리케이션
상단으로

티스토리툴바