title: Calling AlertDialog.Builder in Java For Android Fragment When Back Button Is Pressed
body: <p>I have a simple application (really!) that displays a list and then displays the details of an item on the list based on the user's choice. I do this using Fragments. The details portion is a Fragment which has an <code>EditText</code> in it. Currently, if the user types in the <code>EditText</code> and clicks on the Save button, an <code>AlertDialog.Builder</code> pops up asking her if she wants to save. If she chooses yes, the text is saved to a database. I want the same thing to happen if the user hits the back button. In my class that extends <code>FragmentActivity</code>, I have:</p>
  
  <pre><code>@Override
  public boolean onKeyDown(int keyCode, KeyEvent event)
  {
  
      super.onKeyDown(keyCode, event);
  
      DetailFrag frag = (DetailFrag) getSupportFragmentManager().findFragmentById(R.id.frag_stitchdetail);
      frag.onKeyDown(keyCode, event);
      return false;
  }
  </code></pre>
  
  <p>In my class that extends <code>Fragment</code> (the details portion), I have:</p>
  
  <pre><code>public boolean onKeyDown(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_ENTER &amp;&amp; event.getRepeatCount() == 0
              &amp;&amp; Edited) {
          return true;
      }
      else
          return false;
  }
  </code></pre>
  
  <p>I'm not sure where to put the code that will call the <code>AlertDialog.Builder</code>. I want to put it in the <code>Fragment</code> class because the code needs to get the <code>rowID</code> of the detail from the list (a class that extends <code>ListFragment</code>). That information isn't available to the <code>FragmentActivity</code> class. For clarification, here's the code I use to operate the Save button. It's called from the <code>onCreateView</code> method of the Fragment class and I want to re-use this code (put it in its own method, probably) when the back button is hit.</p>
  
  <pre><code>@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
      View view = inflater.inflate(R.layout.detailfragment, container, false);
      Typeface danielFont = Typeface.createFromAsset(getActivity().getAssets(),
              "danielbk.ttf");
  
      final EditText notes = (EditText)view.findViewById(R.id.stitchnotes);
      notes.setTypeface(danielFont);
      notes.setTextSize(12);
  
      builder = new AlertDialog.Builder(getActivity());
      builder.setMessage("Do you want to save your Notes?");
      builder.setCancelable(false);
  
      builder.setPositiveButton("Yes",
              new DialogInterface.OnClickListener() {
  
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
  
                      String Notes = notes.getText().toString();
                      Uri updateUri = ContentUris.withAppendedId(STITCHES_URI, rowID);
                      ContentValues values = new ContentValues();
                      values.put("stitchnotes", Notes);
                      getActivity().getContentResolver().update(updateUri,values,null,null);
                  }
              });
  
      builder.setNegativeButton("No",
              new DialogInterface.OnClickListener() {
  
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      dialog.cancel();
                      //getActivity().finish();
                  }
              });
  
      alert = builder.create();
  
      ImageButton savebutton = (ImageButton)view.findViewById(R.id.savebutton);
      savebutton.setOnClickListener(new ImageButton.OnClickListener() {
  
          public void onClick(View v) {
              alert.show();
          }
      });
      notes.addTextChangedListener(new TextWatcher() {
  
          @Override
          public void afterTextChanged(Editable s) {
  
          }
  
          @Override
          public void beforeTextChanged(CharSequence s, int start,
                  int count, int after) {
  
          }
  
          @Override
          public void onTextChanged(CharSequence s, int start,
                  int before, int count) {
              Edited = true;
  
          }
      });
  
      return view;
  }
  </code></pre>
  
  <p>The <code>rowID</code> that's used when I declare <code>updateURI</code> comes from the ListFragment class like so:</p>
  
  <pre><code>DetailFrag frag = (DetailFrag) getFragmentManager().findFragmentById(R.id.frag_stitchdetail);
  if (frag != null &amp;&amp; frag.isInLayout()) {
       //more code
       frag.setRowID(stitchid);
  }
  </code></pre>
  
  <p><code>setRowID</code> is defined in the Fragment class:</p>
  
  <pre><code>public void setRowID(int row_id)
  {
      rowID = row_id;
  }
  </code></pre>
  
  <p>and <code>rowID</code> is a private static int in the Fragment class.</p>
  
  <p>Can someone please point me in the right direction?</p>