Title: Cancelling event due to no window focus: KeyEvent
Tags: android;key
Question: I am getting error below when trying to implement backpressed event for application. What does the error below mean?
01-26 14:39:18.400 1983-2430/? E/InitAlarmsService: update failed: java.lang.IllegalArgumentException: Unknown URI content://com.android.calendar/schedule_alarms_remove
01-26 14:39:18.400 2202-2202/com.example.myapp W/ViewRootImpl: Cancelling event due to no window focus: KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x68, repeatCount=0, eventTime=43410, downTime=43400, deviceId=-1, source=0x101 }
01-26 14:39:18.400 2202-2202/com.example.myapp W/ViewRootImpl: Cancelling event due to no window focus: KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x68, repeatCount=0, eventTime=43410, downTime=43400, deviceId=-1, source=0x101 }
01-26 14:39:18.400 2202-2202/com.example.myapp W/ViewRootImpl: Cancelling event due to no window focus: KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x68, repeatCount=0, eventTime=43410, downTime=43400, deviceId=-1, source=0x101 }
Here is the code. I have menus in navigation drawer.Each menu will direct me to the next fragment. after view content in menus, I want to press back key to go back to home page.
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, FragmentFlickrGridImage.OnFragmentInteractionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (savedInstanceState == null) {
Fragment fragment = null;
Class fragmentClass = null;
fragment = new FragmentFlickrGridImage("1");
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setItemIconTintList(null);
navigationView.setNavigationItemSelectedListener(this);
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
onKeyDown(KeyEvent.KEYCODE_BACK, null);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.share) {
return true;
return super.onOptionsItemSelected(item);
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
Class fragmentClass = null;
String title="";
if (id == R.id.one) {
fragment = new FragmentFlickrGridImage("1");
title="One";
} else if (id == R.id.two) {
fragment = new FragmentFlickrGridImage("2");
title ="Two";
} else if (id == R.id.three) {
fragment = new FragmentFlickrGridImage("3");
title="Three";
else if(id==R.id.four){
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).addToBackStack(null).commit();
getSupportActionBar().setTitle(title);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
@Override
public void onFragmentInteraction(Uri uri) {
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK &amp;&amp; event.getRepeatCount() == 0) {
// Add your Dialogue or whatever to alert
moveTaskToBack(true);
return super.onKeyDown(keyCode, event);
Comment: Are you want device backpress event??

Comment: please post your code....

Comment: your code working perfectly to me without moveTaskToBack(true) method()...

Comment: onBackPressed() is not required. Do your stuff on onKeyDown() method.

Comment: Please check this http://stackoverflow.com/questions/13197281/what-is-the-purpose-of-using-movetasktoback-method-in-android

Comment: @Md.ShahadatSarker, I want to backpress event back to homescreen to exit an application. but got this error when test in emulator. The whole emulator was not responsding and restart when i pressed back key event. not sure why..

Comment: @Md.ShahadatSarker, I have posted the code. THank you ..

Comment: @Md.ShahadatSarker, u mean i dont have to call moveTaskToBack(true) function in the onKeyDown?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 8, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:01/26/16', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<android><key>', 'Title': 'Cancelling event due to no window focus: KeyEvent', 'ViewCount': 9976, 'id': 35008381, 'user_AboutMe': None, 'user_AccountId': 5081573.0, 'user_DisplayName': 'michelletbs', 'user_Reputation': 789.0, 'user_Views': 51.0, 'user_id': 5689794.0}}