Jin
Jin

Reputation: 6145

android: ending activity from tab

I have 3 classes, let's call them 1, 2, and 3.

Class 1 extends TabActivity and organizes the whole tab thing, Class 2 and 3 are just two separate tabs each with some lines of text. I call Class 1 from another activity using startActivityForResult.

I then added an optionsMenu in class 2, and when user clicks the optionMenu, the following code is carried out:

@Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        Intent i = new Intent();
        switch(item.getItemId()) {
            case Result.NEXT_ID:
                i.putExtra(Result.PAGE_REQUEST, NEXT);
                setResult(RESULT_OK, i);
                finish();
                break;
            case Result.PREV_ID:
                i.putExtra(Result.PAGE_REQUEST, PREV);
                setResult(RESULT_OK, i);
                finish();
        }
        return super.onMenuItemSelected(featureId, item);
    }

In my parent class (the class that called 1 to begin with), in its onActivityResult function, I want to get the data from the extras. However, the intent is always null, and I can't figure out why.

When I call finish() on class 2, is it calling some other function in class 1? Do I have to transfer the intent data somehow?

Here's the startactivityforresult, Result is Class 1

private void getResult(String result) {
        Intent i = new Intent(this, Result.class);
        i.putExtra(RESULT, result);
        i.putExtra(PAGE, curr_start_page);
        startActivityForResult(i, 0);
    }

I also tried to put the optionmenu methods in Class 1, however, when I call finish(), it doesn't do anything.

Edit:

I think I should put optionsmenu in Class 1, since im starting class 1 in startActivityForResult. But how do I exit out of a tab layout? calling finish() in class 1 doesn't seem to do the trick.

Upvotes: 0

Views: 1058

Answers (2)

Jin
Jin

Reputation: 6145

I just got it. In the optionsmenu method in class 2, I need to do

this.getParent().setResult(RESULT_OK, i);

then call finish(), it works correctly.

Upvotes: 1

Pedro Loureiro
Pedro Loureiro

Reputation: 11514

In class 1, override a onActivityResult and implement it, by doing a setResult with whatever you received.

See if this works.

You're trying to send a value from 2 to 1's parent. But when class 2 finishes, it is just sending its value to class 1. I don't think class 1 is returning this value to its caller.

Upvotes: 0

Related Questions