I want to detect if user pressed home button, i don't need to change it's behaviour. how can i implement it? i tried this solution, but i got exception
Android - Error - IllegalArgumentException: Window type can not be changed after the window is added
source
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME){
mPager.stopAutoScroll();
Log.d("Pager", "Autoscroll stopped");
}
return false;
}
Solution in [dublicate]topic is not working on android l on my device
Correct solution
public class MyApplication extends Application {
public static boolean isActivityVisible() {
return activityVisible;
}
public static void activityResumed() {
activityVisible = true;
}
public static void activityPaused() {
activityVisible = false;
}
private static boolean activityVisible;
}
Register your application class in AndroidManifest.xml:
<application
android:name="your.app.package.MyApplication"
android:icon="@drawable/icon"
android:label="@string/app_name" >
Add onPause and onResume to every Activity in the project (you may create a common ancestor for your Activities if you'd like to, but if your activity is already extended from MapActivity/ListActivity etc. you still need to write the following by hand):
@Override
protected void onResume() {
super.onResume();
mPager.startAutoScroll();
MyApplication.activityResumed();
}
@Override
protected void onPause() {
super.onPause();
MyApplication.activityPaused();
mPager.stopAutoScroll();
}