Robby Smet
Robby Smet

Reputation: 4661

onActivityResult not called the second time

I'm having some problems with my activity's.

I've got a main activity, here I've got 2 methods that are called by a regular class.

public class MainActivity extends Activity {

    public void selectUser(String users) {
    // Start select user screen
    Intent i = new Intent(this, SelectUserActivity.class);
    i.putExtra("users", users);
    //i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivityForResult(i, SELECT_USER_CODE);
}

public void showPincodeScreen(String userName, String residentName) {
    // Start pincode screen
    Intent i = new Intent(this, PincodeActivity.class);
    i.putExtra("userName", userName);
    i.putExtra("residentName", residentName);
    i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(i);
}

}

In the select user screen, the user can choose a user from a list and this user gets returned to the onActivityResult method of the main activity. This works.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_CANCELED) {

        switch (requestCode) {
        case SELECT_RESIDENT_CODE:
            if (data != null) {
                if (data.hasExtra("residentid")) {
                    loadingScreen("Taken worden opgehaald");
                    BroadcastSender.sendValue(
                            data.getStringExtra("residentid"), "1");
                }
            }
            break;

        case SELECT_USER_CODE:
            if (data != null) {
                if (data.hasExtra("userid") && data.hasExtra("username")) {
                    setTitle(data.getStringExtra("username"));
                    loadingScreen.hideMessage();
                    String userid = data.getStringExtra("userid");
                    BroadcastSender.sendValue(userid, "1");
                }
            }
            break;

When the user has been chosen, the showPincodeScreen gets called. But when the user presses a certain button on the pincode screen, the selectUser method is called again and the selectUserActivity gets started again.

But when I select a user now, the pincode screen jumps back to the front and the onActivityResult method of the mainActivity isn't called.

I've already tried to add

i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

to the select user method, but this doesn't work.

What can be the cause?

Edit: This is the finish method of the selectUser Activity. But it works the first time, so I don't think the fault lies here.

    @Override
public void finish() {
    // Prepare data intent
    Intent data = getIntent();
    data.putExtra("userid", selectedItem.getItemID());
    data.putExtra("username", selectedItem.getItemName());
    // Activity finished ok, return the data
    setResult(RESULT_OK, data);
    super.finish();
}

Upvotes: 3

Views: 1588

Answers (2)

Robby Smet
Robby Smet

Reputation: 4661

I found a solution, I placed

@Override
protected void onStop() {
    finish();
    super.onStop();
}

In the pincode activity. When the user now clicks the button, the pincode activity goes to the background, so the onStop method is called and the activity in finished.

Now the onActivityResult of the mainActivity is called and properly executed :)

Upvotes: 2

Tamir Scherzer
Tamir Scherzer

Reputation: 1035

first, you should add SELECT_RESIDENT_CODE to the startActivity at the end of showPincodeScreen so the onActivityResult can catch it's return:

startActivity(i,SELECT_RESIDENT_CODE);

When you call the selectUser activity from the pinCode activity, it will return back to the activity it was called from - pinCode activity. You should implement onActivityResult there and call finish() in it.

Upvotes: 0

Related Questions