android activity call and return result

I have 3 activity A , B and C
Flow is: Activity A calls B , B Call C. (A -> B -> C )
Now I am in Activity C and want to return some result to Activity A without involving B. Scenario is Activity B should be call only by A ( if user press back button from C , B will not be call and activity A will comes in front). Any one let me know how to deal with this situation.

Update :

I am calling activity B from A

Intent intent = new Intent(this, B.class);
            startActivityForResult(intent, 0);

in same way I am calling C from B.

In A i am over riding onActivityResult method

Upvotes: 3

Views: 840

Answers (4)

Dharmendra
Dharmendra

Reputation: 34026

  • start ActivityB as StartActivityForResult from ActivityA.
  • In onCreate() method start ActivityC as StartActivityForResult from ActivityB. Don't finish ActivityB.
  • From ActivityC when work done set the result.
  • In ActivityB when you get the result in the OnActivityResult() set the result to ActivityA and finish ActivityB.
  • From ActivityC override onBackPressed() and set the result null and finish the ActivityC

Upvotes: 2

user370305
user370305

Reputation: 109257

I think you have to call Activity C from B by startActivityForResult() and then in Activity B override onActivityResult() and put finish(); Also from Activity A start Activity B by startActivityForResult() This will direct to Activity A without reCreated it.

Upvotes: 1

Parag Chauhan
Parag Chauhan

Reputation: 35986

This is simple way u do

in Acvtivity C

Intent i=new Intent(getApplicationContext(), A.class);
i.putExtra("id", id);
startActivity();

On backBtn Event

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
       Intent i=new Intent(getApplicationContext(), A.class);
       i.putExtra("id", id);
       startActivity();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

Upvotes: 1

nandeesh
nandeesh

Reputation: 24820

call finish() after startActivity call to C in Activity B

Upvotes: 1

Related Questions