private void savePuzzle() {
    mGame.getCells().markFilledCellsAsNotEditable();

    switch (mState) {
      case STATE_EDIT:
        mDatabase.updateSudoku(mGame);
        Toast.makeText(getApplicationContext(), R.string.puzzle_updated, Toast.LENGTH_SHORT).show();
        break;
      case STATE_INSERT:
        mGame.setCreated(System.currentTimeMillis());
        mDatabase.insertSudoku(mFolderID, mGame);
        Toast.makeText(getApplicationContext(), R.string.puzzle_inserted, Toast.LENGTH_SHORT)
            .show();
        break;
    }
  }
  @Override
  protected void onPause() {
    super.onPause();

    if (isFinishing() && mState != STATE_CANCEL && !mGame.getCells().isEmpty()) {
      savePuzzle();
    }
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // go fullscreen for devices with QVGA screen (only way I found
    // how to fit UI on the screen)
    Display display = getWindowManager().getDefaultDisplay();
    if ((display.getWidth() == 240 || display.getWidth() == 320)
        && (display.getHeight() == 240 || display.getHeight() == 320)) {
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      getWindow()
          .setFlags(
              WindowManager.LayoutParams.FLAG_FULLSCREEN,
              WindowManager.LayoutParams.FLAG_FULLSCREEN);
      mFullScreen = true;
    }

    // theme must be set before setContentView
    AndroidUtils.setThemeFromPreferences(this);

    setContentView(R.layout.sudoku_edit);
    mRootLayout = (ViewGroup) findViewById(R.id.root_layout);
    mBoard = (SudokuBoardView) findViewById(R.id.sudoku_board);

    mDatabase = new SudokuDatabase(getApplicationContext());

    mGuiHandler = new Handler();

    Intent intent = getIntent();
    String action = intent.getAction();
    if (Intent.ACTION_EDIT.equals(action)) {
      // Requested to edit: set that state, and the data being edited.
      mState = STATE_EDIT;
      if (intent.hasExtra(EXTRA_SUDOKU_ID)) {
        mSudokuID = intent.getLongExtra(EXTRA_SUDOKU_ID, 0);
      } else {
        throw new IllegalArgumentException(
            String.format("Extra with key '%s' is required.", EXTRA_SUDOKU_ID));
      }
    } else if (Intent.ACTION_INSERT.equals(action)) {
      mState = STATE_INSERT;
      mSudokuID = 0;

      if (intent.hasExtra(EXTRA_FOLDER_ID)) {
        mFolderID = intent.getLongExtra(EXTRA_FOLDER_ID, 0);
      } else {
        throw new IllegalArgumentException(
            String.format("Extra with key '%s' is required.", EXTRA_FOLDER_ID));
      }

    } else {
      // Whoops, unknown action!  Bail.
      Log.e(TAG, "Unknown action, exiting.");
      finish();
      return;
    }

    if (savedInstanceState != null) {
      mGame = new SudokuGame();
      mGame.restoreState(savedInstanceState);
    } else {
      if (mSudokuID != 0) {
        // existing sudoku, read it from database
        mGame = mDatabase.getSudoku(mSudokuID);
        mGame.getCells().markAllCellsAsEditable();
      } else {
        mGame = SudokuGame.createEmptyGame();
      }
    }
    mBoard.setGame(mGame);

    mInputMethods = (IMControlPanel) findViewById(R.id.input_methods);
    mInputMethods.initialize(mBoard, mGame, null);

    // only numpad input method will be enabled
    for (InputMethod im : mInputMethods.getInputMethods()) {
      im.setEnabled(false);
    }
    mInputMethods.getInputMethod().setEnabled(true);
    mInputMethods.activateInputMethod();
  }
  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    mGame.saveState(outState);
  }