Skip to content

Instantly share code, notes, and snippets.

@HassakuTb
Created November 4, 2016 09:26
Show Gist options
  • Select an option

  • Save HassakuTb/1648e625b0127a46aeec60c678185a05 to your computer and use it in GitHub Desktop.

Select an option

Save HassakuTb/1648e625b0127a46aeec60c678185a05 to your computer and use it in GitHub Desktop.
Checking if the Android application was in foreground.
package jp.tangerinebox.check_is_foreground;
import android.app.Activity;
public abstract class BaseActivity extends Activity{
@Override
protected void onStart(){
super.onStart();
HogeApplication.getInstance().onActivityStart();
}
@Override
protected void onStop(){
super.onStop();
HogeApplication.getInstance().onActivityStop();
}
}
package jp.tangerinebox.check_is_foreground;
import android.app.Application;
public class HogeApplication extends Application{
// ==== Singleton用 ====
private static HogeApplication sInstance = null;
public static HogeApplication getInstance(){
return sInstance;
}
@Override
public void onCreate(){
super.onCreate();
sInstance = this;
}
// ==== フォアグラウンドチェック ====
private int mStartCount = 0;
private int mStopCount = 0;
void onActivityStart(){
++ mStartCount;
}
void onActivityStop(){
++ mStopCount;
}
public boolean isForeground(){
return mStartCount > mStopCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment