Cancelling event due to no window focus: KeyEvent
<p>I am getting error below when trying to implement backpressed event for application. What does the error below mean? </p>

<pre><code> 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 }
</code></pre>

<p>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.</p>

<pre><code>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);
}
</code></pre>