chris
chris

Reputation: 729

How to maintain startActivityForResult for single activity

I have one scenario to main with activities..

I have three activities A, B, C

Now I am calling C from A

A-->C

and in C I have onBackPress

Same way using startActivity for result

B-->C

So in my A and B I am using StartActitivity for result and maintaining in OnActivityForResult..

Activity A

Intent i = new Intent(this, AddNewAddress.class);
i.putExtra("addresstype", addressType);
startActivityForResult(i, 10);

And in OnActivityResult of A

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        // check if the request code is same as what is passed  here it is 2


        switch (requestCode) {

            case (10): {
                // do this if request code is 10.
                addressType=data.getStringExtra("MESSAGE");
                getAddressList(userId,addressType);
                mAdapter.notifyDataSetChanged();
                addrListModels.clear();
            }
            break;


        }

    }

Same thing I am doing in B as well....

Now in C

 @Override
    public void onBackPressed() {
        super.onBackPressed();

        Intent intent=new Intent();
        intent.putExtra("MESSAGE",addresstype);
        setResult(10,intent);
        finish();
    }

And crash is happening

java.lang.RuntimeException: Failure delivering result ResultInfo

Upvotes: 0

Views: 221

Answers (3)

Ferran
Ferran

Reputation: 1448

@jiajiagu response is ok, don't call super.onBackPressed if you use finish.

However, as @mayur-panchalwhen try to say, if you use startActivityForResult, you don't need to return the requestCode. The system already knows which Activity is returning.

You need to return the resultCode.

ResultCode could be RESULT_OK or RESULT_CANCELED.

So, when finishing an Activity use

    setResult(RESULT_OK, intent);
    finish();

In onActivityResult test requestCode to identify which Activity is returning and resultCode to know if the result was successful.

Upvotes: 0

JiajiaGu
JiajiaGu

Reputation: 1339

Remove super.onBackPressed(); in C's onBackPressed method,please have a try.

Upvotes: 1

Mayur
Mayur

Reputation: 735

Intent i = new Intent(this, AddNewAddress.class);

            i.putExtra("addresstype", addressType);
            startActivityForResult(i, 10);

    @Override
        public void onBackPressed() {
            super.onBackPressed();

            Intent intent=new Intent();
            intent.putExtra("MESSAGE",addresstype);
            setResult(RESULT_OK,intent);
            finish();
        }

    onActivityResult use RESULT_OK

        @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (resultCode == RESULT_OK) {
                    switch (requestCode) {
                        case 10:
                    }
                }
            }

Upvotes: 0

Related Questions