title: Android how to stop recursive function when I got one result
body: <p>I am new with Android. I am writing simple app that help user solve Sudoku. User will be asked to input some numbers my program will provide only ONE solution for that puzzle. </p>
  
  <p>My recursive function to find results works properly but I dont know how to stop that function when I got first solution. Therefore, my app keep running and then crash.</p>
  
  <p>Here is my code</p>
  
  <pre><code>public class Solver extends ActionBarActivity implements OnClickListener{
  
  private final String TAG = "Solver";
  public final int[][] a = new int[9][9];
  private SolverView solverView;
  public static final int[][] temp = {
      {1,1,1,2,2,2,3,3,3},
      {1,1,1,2,2,2,3,3,3},
      {1,1,1,2,2,2,3,3,3},
      {4,4,4,5,5,5,6,6,6},
      {4,4,4,5,5,5,6,6,6},
      {4,4,4,5,5,5,6,6,6},
      {7,7,7,8,8,8,9,9,9},
      {7,7,7,8,8,8,9,9,9},
      {7,7,7,8,8,8,9,9,9}
  };
  
  @Override
  protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      solverView = new SolverView(this);
      setContentView(R.layout.activity_solver);
      solverView.requestFocus();
      solverView.bringToFront();
      setContentView(solverView);
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.solver, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      int id = item.getItemId();
      if (id == R.id.action_settings) {
          Log.d(TAG, "menuSelect: do something");
          run(a, 0);
          return true;
      }
      return super.onOptionsItemSelected(item);
  }
  
  @Override
  public void onClick(View v) {
      // TODO Auto-generated method stub
      if (v.getId() == R.id.solve) {
          Log.d(TAG, "Solve button pressed");
      }
  }
  
  protected void showKeypad(int x, int y) {
      Dialog dialog = new Keyboard(this, this.solverView);
      dialog.show();
  }
  
  public boolean setCellIfValid(int selU, int selV, int t) {
      Log.d(TAG, " in setCellIfValid " + selU + " " + selV + " " + t);
      this.a[selU][selV] = t;
      solverView.invalidate();
      return true;
  }
  
  public String getCellString(int i, int j) {
      int v = this.a[i][j];
  
      if (v == 0)
          return "";
      else
          return String.valueOf(v);
  
  }
  
  public static void run(int[][] a, int n) {
      if (n &gt;= 81) {
          printArray(a);
      // I WANT MY PROGRAM STOP HERE
          return;
      }
      int i = n / 9;
      int j = n % 9;
      if (a[i][j] != 0) run(a, n + 1);
      for (int k = 1; k &lt;= 9; k++) 
          if (valid(a, i, j, k)) {
              a[i][j] = k;
              run(a, n + 1);
              a[i][j] = 0;
          }
  }
  
  public static boolean valid(int[][] a, int u, int v, int k) {
      //check column
      for (int i = 0; i &lt; 9; i++)
          if (a[i][v] == k) return false;
      //check row
      for (int j = 0; j &lt; 9; j++)
          if (a[u][j] == k) return false;
      //check small square
      for (int i = 0; i &lt; 9; i++)
          for (int j = 0; j &lt; 9; j++)
              if (temp[i][j] == temp[u][v] &amp;&amp; a[i][j] == k)
                  return false;
      return true;
  
  }
  
  public static void printArray(int[][] a) {
      for (int i = 0; i &lt; a.length; i++) {
          for (int j = 0; j &lt; a[0].length; j++)
              System.out.print(a[i][j] + " ");
          System.out.println();
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Anyone could help me? Thank you so much for reading.
  Sincerely</p>