问题
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 setResult(RESULT_OK);
Everything works fine if the username is entered by clicking the 'addusername' button.
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)
Here is the code for 'addusername' activity:
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();
}
Here is the code for Dialog:
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();
}
}
I understand this is happening because the Dialog does not call startActivityForResult() method. But even if I change my code, like this:
Intent intent = new Intent(getActivity().getBaseContext(), Editprofile.class);
startActivityForResult(intent, 0);
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.
EDIT 1: Tried using
Intent intent = new Intent(getActivity(), Editprofile.class);
startActivityForResult(intent, 0);
No use.
回答1:
Have you tried this way:
Intent intent = new Intent(getActivity(), Editprofile.class);
startActivityForResult(intent, 0);
When you create your the Intent object you use the BaseContext, instead of using the reference to the activity itself.
回答2:
Using
Intent intent = new Intent(getActivity(), Editprofile.class);
getActivity().startActivityForResult(intent, 0);
worked!
来源:https://stackoverflow.com/questions/14038436/how-to-use-startactivityforresult-through-a-dialogfragment