티스토리 뷰
Background 상태에서 Foregound상태 전환 시 앱 새로 시작하기
Dexx 2016. 6. 15. 12:32앱을 사용하다 홈버튼을 누르거나 다른 앱으로 이동한 후 다시 우리 앱으로 돌아왔을 때 앱을 새로 로딩하고 싶었다.
첨에는 lifeCycle을 이용하면 될거 같아 시도해 보았지만 앱내에서 Activity를 띄우는 것만으로도 Background로 인식하는 바람에 다른 방법을 찾다가
우연히 발견하게 되었다.
역시 없는게 없어~
결론은 액티비티 생명주기가 아니라 Application에 있었다.
출처 : http://yslibrary.net/2015/07/30/android_how_to_detect_app_is_background_or_not/
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | public class MyApplication extends Application { private AppStatus mAppStatus = AppStatus.FOREGROUND; @Override public void onCreate() { super.onCreate(); registerActivityLifecycleCallbacks (new MyActivityLifecycleCallbacks ()); } public MyApplication get (Context context) { return (MyApplication) context.getApplicationContext (); } public AppStatus getAppStatus () { return mAppStatus; } // check if app is foreground public boolean isForeground () { return mAppStatus.ordinal ()> AppStatus.BACKGROUND.ordinal(); } public enum AppStatus { BACKGROUND, // app is background RETURNED_TO_FOREGROUND, // app returned to foreground (or first launch) FOREGROUND; // app is foreground } public class MyActivityLifecycleCallbacks implements ActivityLifecycleCallbacks { // running activity count private int running = 0; @Override public void onActivityCreated (Activity activity, Bundle bundle) { } @Override public void onActivityStarted (Activity activity) { if (++ running == 1) { // running activity is 1 // app must be returned from background just now (or first launch) mAppStatus = AppStatus.RETURNED_TO_FOREGROUND; } else if (running> 1) { // 2 or more running activities, // should be foreground already. mAppStatus = AppStatus.FOREGROUND; } } @Override public void onActivityResumed (Activity activity) { } @Override public void onActivityPaused (Activity activity) { } @Override public void onActivityStopped (Activity activity) { if (--running == 0) { // no active activity // app goes to background mAppStatus = AppStatus.BACKGROUND; } } @Override public void onActivitySaveInstanceState (Activity activity, Bundle bundle) { } @Override public void onActivityDestroyed (Activity activity) { } } } | cs |
백그라운드 상태인지 체크하고 싶은 곳에서 호출
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @Override protected void onResume() { super.onResume(); if (mApplication.get(this).getAppStatus() == MyApplication.AppStatus.RETURNED_TO_FOREGROUND) { Log.d(TAG, "백그라운드에서 돌아왔다"); } } | cs |
mApplication.get(this).getAppStatus() 앱의 현재 상태
MyApplication.AppStatus.RETURNED_TO_FOREGROUND 백그라운드에서 돌아왔는지 체크
현재 상태와 앱상태가 같다면 새로시작..
이외 BACKGROUND와 FOREGROUND 상태를 체크할 .수 있으니 다른 용도로도 사용 가능 할 듯.
<!-- 추가 -->
백그라운드에서 돌아왔을 때 새로 시작하는 부분을 기존의 intent를 썼더니 2중으로 UI가 실행되는 문제가 발생
해당 부분을 다음으로 변경하였다.
일단 아직까진 2중 실행되지 않는데...좀더 지켜봐야 할 듯.. 예를들어 Notification Push에서 들어올때 라든가...
Intent reStartIntent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
reStartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(reStartIntent);
finish();
'Android 개발 메모' 카테고리의 다른 글
페이스북 공유기하기 (0) | 2016.06.23 |
---|---|
구글 애널리틱스 적용하기 (0) | 2016.06.02 |
커스텀 프로그래스바 만들기 (0) | 2016.05.20 |