본문 바로가기
My Image
프로그래밍/Android

[Android] 화면에 계속 앱 띄우는 방법

by Lim-Ky 2017. 5. 22.
반응형

안드로이드앱을 개발하다보면...


홈키 또는 백키를 눌렀을 때 항상 내가 실행시키고 있는 앱이 종료되지 않고, 최상위로 실행되게 하고 싶을 때가 있다.


여러 방법이 있겠지만,


액티비티에 백키이벤트를 오버라이딩해서 내가 새롭게 백키 이벤트를 만들고 

(보통 매인액티비티하나의 여러개의 프래그먼트를 구성하는 방법을 사용한다.)


매니패스트파일에서 앱을 런처앱이자 홈앱으로 설정하면 홈키를 눌러도 해당앱이 화면에서 없어지지 않게 할 수 있다.



1. 우선 백키를 오버라이딩해서 내 입맛에 맞게 적용한다.


@Override
public void onBackPressed(){

backButtonFunction();

}

public void backButtonFunction(){
ColorManager.getInstance().startFlag = true;
int backStackCount = getSupportFragmentManager().getBackStackEntryCount();
// Toast.makeText(getApplicationContext(), ""+backStackCount, Toast.LENGTH_SHORT).show();
//currentTimeMillis 현재시간이 버튼을 눌린 시간 + 2초 보다 흘럿다면 2초내 클릭 안한것임.
if(backStackCount == 1) return;

if(System.currentTimeMillis() > backKeyPressedTime + 2000){

//backKeyPressedTime 버튼을 누른 시간을 입력
backKeyPressedTime = System.currentTimeMillis();


super.onBackPressed();
return;
}
if(System.currentTimeMillis() <= backKeyPressedTime + 2000){
// finish();
// toast.cancel();
}

if (backStackCount > 0) {
// FragmentUtil.goBack();
super.onBackPressed();
}

}

뭐 이리 복잡해보여 할 수 있는데.... 흠...2초내에 더블클릭을 하면 앱이 종료되게 하는 백키이벤트를 지우지않아서 복잡해보일 수 있다..


다른거 다 필요없고 여기서 참고할 부분은


backStackCount가 1일 경우 스택에 메인프래그먼트가 하나만 띄어져 있는 경우에 백키를 누르면 정상적으론 앱이 종료되지만,


위에 코드처럼 강제로 리턴시켜 매인프래그먼트가 화면상에서 사라지지 않게 막을 수 있다.






2. 매니패스트에서 인텐트필터안에 카테고리 홈과 디폴트를 추가한다.


<activity android:name=".Activity.MainActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>



우선 임시적으로 이 두가지를 적용하면, 홈키와 백키를 눌렀을 경우에도 실행시키는 앱이 화면상에 계속 나올 수 있다.








반응형

댓글