모바일/안드로이드앱

안드로이드앱 태스크 Task , onNewIntent() ,singleTop / Android Studio / 안드로이드 앱만들기 24

yy_dd2 2021. 3. 13. 18:52
반응형

안드로이드  / Android Studio / 안드로이드 앱만들기태스크 관리
Task
앱을 실행하면 앱은 프로세스 위에서 동작한다
프로세스 하나가 실행되고 VM(virtual Machine가상머신)이 만들어지고 또 VM(가상머신) 위에서 실행된다
프로세스는 독립적인 화면인데 프로세스 간의 정보공유가 어려워서 태스크라는 것이 있다.
- 태스크 : 앱이 어떻게 동작할지 결정하는데 사용됨 (독립적인 실행 단위와 상관없이 어떤 화면들이 같이 동작해야 하는지 흐름을 관리함)

앱의 화면을 띄우지 않고 전화앱을 실행하면 전화앱과 앱의 태스크는 별도로 생겨나는데
시스템에서 알아서 태스크를 관리하지만 직접 태스크를 관리하는 경우가 생긴다고함(언제???)

- 매니페스트(AndroidManifest.xml)파일에서 액티비티를 등록할 때 태스크도 설정할수있다.

 

 

태스크 설정해보기
1. SampleTask 프로젝트 생성
2. activity_main.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=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="첫 번째 화면"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="나 자신 띄우기"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

</androidx.constraintlayout.widget.ConstraintLayout>


3. MainActivity.java 버튼 누를때 인텐트를 사용해 MainActivity화면을 띄우게 코드 작성

package com.togapp.sampletask;

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);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

 

--> 이렇게 작성하면 화면 실행하고나면 버튼을 누르면

새 Main화면이 계속 뜬다 뒤로가기 버튼을 누르면 띄웠던거 전으로 이동함

 

4. AndroidManifest.xml 파일에서 

android:launchMode="singleTop"

이렇게 설정값을 주면 가장 위쪽에 있는 액티비티를 새로만들지 않는다.

플래그 FLAG_ACTIVITY_SINGLE_TOP 으로 설정하는거와 같다 tog-code.tistory.com/46

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

    <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=".MainActivity"
                    android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

- 이렇게되면 플래그에서 말했던것처럼 MainActivity로 전달되는 인텐트는 onNewIntent() 메서드를 사용해서 전달해야한다

 

- AndroidManifest.xml 파일에서 <activity> 태그 launchMode 속성 값을 singleTask로 설정하면 이 액티비티가 실행되는 시점에서 새로운 태스크를 만들게된다

- singleInstance로 설정하면 액티비티가 실행되는 시점에서 새로운 태스크를 만들고 이후 실행되는 액티비티에 태스크를 공유하지 않게 된다.

 

----태스크를 잘 사용해야 앱의 메모리 사용을 잘 쓸수있는거 같다.

 

 

반응형