본문 바로가기
안드로이드/개발자 일상

안드로이드 ActivityManager (제일 위에 있는 Activity 알아보기)

by 차누감 2020. 4. 10.

현재 보이는 Activity에서 새로운 Activity를 호출하면, 현재 보이는 화면 위에 쌓이는 형태가 됩니다.

이럴 경우 제일 위에 쌓은 화면이 무엇인지 궁금할 경우가 있습니다.

 

<실행 화면> 원할 때에 가장 위에 쌓인 액티비티를 구분할 수 있습니다.


버튼을 눌렀을 경우 새로운 Activity를 띄우는 예제입니다.

activity_main.xml 코드 ( 접는 글 )

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="utf-8"?>
    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="MainActivity"
        android:textSize="32sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click_newActivity"
        android:text="new activity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
 
 
 

새로 띄울 SecondActivity를 만들어 줍니다.

SecondActivity의 xml은 아래와 같이 구분을 위해 표기를 했습니다.

<핵심 코드> topActivityName을 알아낼 수 있습니다.

1
2
3
4
         ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> info = manager.getRunningTasks(1);
        ComponentName componentName= info.get(0).topActivity;
        String topActivityName = componentName.getShortClassName().substring(1);

예제에서는

MainActivity 위에 새로운 화면이 쌓였을 때 ( 즉, MainActivity 가려졌을때,  onStop()에 코드 기재 )

MainActivity 위에 새로운 화면이 없어졌을 때 ( 즉, MainActivity 보일때, onResume()에 코드 기재 )

두 가지 경우에 위 코드를 적용해보겠습니다.

 

MainActivity.java 코드 ( 접는 글 )

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.example.topactivity;
 
 
import android.content.ComponentName;
import android.content.Context;
 
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }// onCreate()..
 
    @Override
    protected void onResume() { // 액티비티가 화면에서 완전히 보일때 호출
        super.onResume();
 
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> info = manager.getRunningTasks(1);
        ComponentName componentName= info.get(0).topActivity;
        String topActivityName = componentName.getShortClassName().substring(1);
        Toast.makeText(this, topActivityName, Toast.LENGTH_SHORT).show();
    }
 
    @Override
    protected void onStop() {   // 액티비티가 화면에 완전히 가려졌을때 호출
        super.onStop();
 
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> info = manager.getRunningTasks(1);
        ComponentName componentName= info.get(0).topActivity;
        String topActivityName = componentName.getShortClassName().substring(1);
        Toast.makeText(this, topActivityName, Toast.LENGTH_SHORT).show();
 
    }// onStop()..
 
    public void click_newActivity(View view) {
        Intent intent=new Intent(this, SecondActivity.class);
        startActivity(intent);
    }
}// MainActivity class..
 
 

<실행 화면> 원할때에 가장 위에 쌓인 액티비티를 구분할 수 있습니다.

댓글