title: How to use startActivityForResult() through a DialogFragment?
body: <p>My app requires a username to be added to function properly. 
  The mainActivity displays the username on the top which it retrieves form the database. 
  The mainActivity also has a button which leads to 'addusername' activity through startActivityForResult() method; when the user actually enters a username then only username on the main activity gets refreshed and displayed.
  There is a submit button on the 'addusername' activity which adds the username and does <code>setResult(RESULT_OK);</code><br>
  Everything works fine if the username is entered by clicking the 'addusername' button. </p>
  
  <p>Now, I have added a dialogue in mainActivity which gets displayed when the app starts only if no username exists on the database. The dialogue gives an option to add username, which if clicked, leads to the 'addusername' activity. But upon pressing the 'submit' button the mainActivity does get called but the username is not refreshed (though, the database does get updated) </p>
  
  <p>Here is the code for 'addusername' activity:</p>
  
  <pre><code>public void submitusername(View view) {
  
      userdatabase name = new userdatabase(this);
      name.open();
      String user = name.getusername();
      name.close();
  
      EditText cinone = (EditText) findViewById(R.id.username);
      username = cinone.getText().toString();
  
      if(user.equals("")) {
  
          userdatabase entry = new userdatabase(Editprofile.this);
          entry.open();
          entry.createEntry(username, null, null, null);
          entry.close();
  
          Context context = getApplicationContext();
          CharSequence text = "Added new user!";
          int duration = Toast.LENGTH_LONG;
  
          Toast toast = Toast.makeText(context, text, duration);
          toast.show();
  
      }
  
      else {
  
          userdatabase update = new userdatabase(Editprofile.this);
          update.open();
          update.updateUsername(username);
          update.close();
  
          Context context = getApplicationContext();
          CharSequence text = "Username updated!";
          int duration = Toast.LENGTH_SHORT;
  
          Toast toast = Toast.makeText(context, text, duration);
          toast.show();
  
      }
  
      setResult(RESULT_OK);       
      finish();
  
  }
  </code></pre>
  
  <p>Here is the code for Dialog:</p>
  
  <pre><code>public class NouserFragment extends DialogFragment {
  
  
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
  
      // Use the Builder class for convenient dialog construction
  
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
      builder.setMessage(R.string.nouseralert)
             .setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
  
                     Intent intent = new Intent(getActivity().getBaseContext(), Editprofile.class);    
                     startActivity(intent);    
  
                 }
             })
             .setNegativeButton(R.string.ignore, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                     // User cancelled the dialog
                 }
             });
      // Create the AlertDialog object and return it
      return builder.create();
  }
  }
  </code></pre>
  
  <p>I understand  this is happening because the Dialog does not call startActivityForResult() method. But even if I change my code, like this:</p>
  
  <pre><code>Intent intent = new Intent(getActivity().getBaseContext(), Editprofile.class);    
                     startActivityForResult(intent, 0);
  </code></pre>
  
  <p>It still does not help. Probably because, onActivityResult() method does not lie in the same class from which startActivityForResult() has been called, but I don't know how to get after that.  </p>
  
  <p>EDIT 1: Tried using </p>
  
  <pre><code>Intent intent = new Intent(getActivity(), Editprofile.class);
  startActivityForResult(intent, 0);
  </code></pre>
  
  <p>No use.</p>