Computer >> 컴퓨터 >  >> 프로그래밍 >> Android

Android에서 액티비티를 포그라운드로 가져오는 방법 (작업 스택 최상위로 이동)

Android에서 액티비티를 포그라운드(스택 최상위)로 가져오기

이 튜토리얼에서는 FLAG_ACTIVITY_REORDER_TO_FRONT 플래그를 사용하여 Android에서 이미 실행 중인 액티비티를 포그라운드, 즉 작업 스택의 최상위로 가져오는 방법을 단계별로 알아봅니다.

이 플래그는 새로운 액티비티 인스턴스를 생성하지 않고, 기존에 백그라운드에 있던 액티비티를 스택의 맨 앞으로 재정렬해주기 때문에 메모리 사용 측면에서도 효율적입니다.

1단계 — 새 프로젝트 생성

Android Studio에서 File ⇒ New Project를 선택하고, 필요한 모든 정보를 입력하여 새 프로젝트를 생성합니다.

2단계 — activity_main.xml 코드 작성

res/layout/activity_main.xml 파일에 아래 코드를 추가합니다.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
   xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:app="https://schemas.android.com/apk/res-auto"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Hello World!"
   app:layout_constraintBottom_toBottomOf="parent"
   app:layout_constraintLeft_toLeftOf="parent"
   app:layout_constraintRight_toRightOf="parent"
   app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

3단계 — MainActivity.java 코드 작성

src/MainActivity.java 파일에 아래 코드를 추가합니다. 핵심은 Intent.FLAG_ACTIVITY_REORDER_TO_FRONT 플래그를 설정하는 부분입니다.

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Intent i = new Intent(this, MyActivity.class);
      i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
      startActivity(i);
  }
}

4단계 — MyActivity 생성

새로운 액티비티(MyActivity.java)를 생성하고 아래 코드를 추가합니다.

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MyActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_my_acitivity);
  }
}

5단계 — activity_myacitivity.xml 코드 작성

activity_myacitivity.xml 레이아웃 파일에 아래 코드를 추가합니다.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MyActivity">
<TextView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="MYACTIVITY"
   android:layout_centerInParent="true"
   android:textAlignment="center"
   android:textSize="24sp"
   android:textStyle="bold"/>
</RelativeLayout>

6단계 — AndroidManifest.xml 수정

AndroidManifest.xml 파일에 아래와 같이 두 액티비티를 등록합니다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
   package="app.com.sample">
   <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/AppTheme">
      <activity android:name=".MyActivity"/>
      <activity android:name=".MainActivity">
          <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
      </activity>
   </application>
</manifest>

앱 실행 및 결과 확인

이제 애플리케이션을 실행해 보겠습니다. 실제 Android 기기를 컴퓨터에 연결했다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 툴바에서 Run 아이콘을 클릭합니다.

Android에서 액티비티를 포그라운드로 가져오는 방법 (작업 스택 최상위로 이동)

실행 옵션에서 자신의 모바일 기기를 선택하면, 연결된 기본 화면이 다음과 같이 표시됩니다.

Android에서 액티비티를 포그라운드로 가져오는 방법 (작업 스택 최상위로 이동)