I have written an Activity using Theme.Holo.Dialog
so that it works as an AlertDialog
as a login/password notice. I've started this activity with startActivityForResult(...)
using a request code I defined. The thing is, whenever I start the activity onActivityResult(...)
is triggered immediately, the buttons get loaded and everything, but once I press them, even though I know the Activity is working because login does happen, there is no result sent back to the first Activity and I am calling setResult(...)
and finish()
after the buttons are pressed.
Thanks in advance, first time using startActivityForResult so I'm sure I must be missing something.
Main Activity:
.
.
.
Intent startDialogIntent = new Intent(this,SetIdDialogAlertActivity.class);
startDialogIntent.setAction(getString(R.string.ACTION_START_ALERT_DIALOG_ACTIVITY));
startDialogIntent.putExtra(getString(R.string.parcelable_status));
IntentParcelable.configurationToParcelable(configuration, getBaseContext()));
startActivityForResult(startDialogIntent,getResources().getInteger(R.integer.CREATE_USER));
.
.
.
public void onActivityResult(int requestCode, int resultCode,Intent data){
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(getBaseContext(),"returned from activity with request code " + requestCode + " and result code " + resultCode, Toast.LENGTH_SHORT).show();
if(requestCode == getResources().getInteger(R.integer.CREATE_USER))
switch (resultCode){
case RESULT_NEW_USER:
Toast.makeText(getBaseContext(),getString(R.string.SU_CorrectPassword_message), Toast.LENGTH_SHORT).show();
refreshLabels(data);
break;
case RESULT_WRONG_PASSWORD:
Toast.makeText(getBaseContext(),getString(R.string.SU_WrongPassword_message), Toast.LENGTH_SHORT).show();
break;
}
}
Then on the sub-activity:
public void onCreate(Bundle savedInstanceState){
Logger.d(Messages.tag,System.nanoTime() + "_DIALOG_ALERT:_ON_CREATED");
super.onCreate(savedInstanceState);
setContentView(R.layout.user_id_alertdialog);
Logger.d(Messages.tag,System.nanoTime() + "_DIALOG_ALERT:_VIEW_INFLATED");
SU_passwordEditText = (EditText)findViewById(R.id.SU_passwordEditText);
Button okAlarmButton = (Button)findViewById(R.id.okAlarmButton);
Button cancelAlarmButton = (Button)findViewById(R.id.cancelAlarmButton);
Logger.d(Messages.tag,System.nanoTime() + "_DIALOG_ALERT:_OK_BUTTON_LOADED");
okAlarmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Logger.d(Messages.tag,System.nanoTime() + "_DIALOG_ALERT:_OK_BUTTON_PRESSED");
String password = SU_passwordEditText.getText().toString();
if(password.equals(getString(R.string.SU_password))){
Toast.makeText(getBaseContext(),getString(R.string.SU_CorrectPassword_message), Toast.LENGTH_SHORT).show();
userIdEditText = (EditText)findViewById(R.id.userIdEditText);
String newUserName = userIdEditText.getText().toString();
Logger.d(Messages.tag,System.nanoTime() + "_DIALOG_ALERT:_USERNAME_" + newUserName);
Configuration configuration = IntentParcelable.parcelableToConfiguration((IntentParcelable)getIntent().getParcelableExtra(getString(R.string.parcelable_status))
, getBaseContext());
configuration.setId(newUserName);
Intent setUserIntent = new Intent();
setUserIntent.setAction(getString(R.string.ACTION_ACTIVITY_NEW_ID));
setUserIntent.putExtra(getString(R.string.INTENT_EXTRA_SENDER_NAME),"DIALOG_ACTIVITY");
setUserIntent.putExtra(getString(R.string.INTENT_EXTRA_NEW_CONFIG), IntentParcelable.configurationToParcelable(configuration, getBaseContext()));
exitActivity(RESULT_NEW_USER,setUserIntent);
}else{
Toast.makeText(getBaseContext(),getString(R.string.SU_WrongPassword_message), Toast.LENGTH_SHORT).show();
exitActivity(RESULT_WRONG_PASSWORD);
}
}
});
private void exitActivity(int exit_code, Intent data) {
setResult(exit_code,data);
finish();
}
private void exitActivity(int exit_code) {
setResult(exit_code);
finish();
}
Sorry for the delay in posting the code.
[UPDATE]
I've tried everything I can come up with and the sub-Activity is still behaving weirdly. I thought it was finishing abruptly since the parent's onActivityResult(...)
triggers. It does not finish because it's still on top of the stack, I can see it, and it does work since anything I implement into the Button
s gets done. But when it finishes again it doesn't trigger the previous one's onActivityResult(...)
method.
[UPDATE]
I MUST be missing something. I just created a whole new Activity
, for testing's sake, that does nothing but creating itself, setting the result to 4 and finishing. I declared it on the manifest as:
<activity
android:name=".ActivityTest"
android:screenOrientation="portrait"
android:process=":gui_process"
android:exported="false">
</activity>
And started it like this:
Intent startDialogIntent = new Intent(this,ActivityTest.class);
startActivityForResult(startDialogIntent,getResources().getInteger(R.integer.CREATE_USER));
Defined a new case statement on my onActivityResult(...)
and it behaves just like the original activity.