2

In my manifest.xml, i'm used android:parentActivityName to set up parent Activity. Here my code:

<activity
            android:name=".B"
            android:label="@string/title_activity_A"
            android:parentActivityName=".A" >
        </activity>

So, how to put data from B to A when click back button in action bar?? Because my A activity is child activity of another activity(C activity) and A activity using data from this activity(C activity)... Somebody can help me please? Thanks and sorry because my english.

Hades10
  • 53
  • 2
  • 8
  • Check this https://stackoverflow.com/questions/14292398/how-to-pass-data-from-2nd-activity-to-1st-activity-when-pressed-back-android – Brinda Rathod Oct 01 '18 at 12:30

2 Answers2

1

In your Activity B class write:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        Intent intent= new Intent();
        intent.putExtra("param", "value");
        setResult(RESULT_OK, intent);
        finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

In Activity A class add:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK && requestCode == 2404) {
        if(data != null) {
            String value = data.getStringExtra("param");
        }
    }
}

Start an Activity B using startActivityForResult Method

Intent intent = new Intent(A.this.getApplicationContext(), B.class);
startActivityForResult(intent, 2404);
Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46
  • When you click back button on parent activity. set the intent and you will receive same intent in onActivityResult method. – Dhaval Patel Apr 15 '15 at 11:46
  • 2404 is just request code to uniquely identify parent activity. If request code>= 0, this code will be returned in onActivityResult() when the parent activity exits – Dhaval Patel Apr 15 '15 at 11:48
  • @user3706741 I have updated the answer. Check if it works for you. – Dhaval Patel Apr 15 '15 at 12:00
0

You have to Override the actionbar back button click event & set data to parent activity with Intent parsing & handle in parent activity in onActivityResult() method.