简体   繁体   中英

onActivityResult not called when pressed back arrow on screen but only called when back button pressed in android?

I have two activities one is parent and one is child. I have set parent of child in android manifest:

<activity android:name=".SecondActivity"
            android:parentActivityName=".MainActivity"
            ></activity>

Parent:

package com.kofhearts.activityresulttest;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to

        Log.w("result", "result");
    }

    public void handleClick(View view){


        Intent intent = new Intent(this, SecondActivity.class);
        startActivityForResult(intent, 1);


    }



}

Child:

package com.kofhearts.activityresulttest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }


}

When in child activity when i press back button in android then the onActivityResult is called denoted by log message. When I press back arrow then onActivityResult is not called. Why is this the case and how can i make it so that no matter the method employed to go back, the onActivityResult is called?

UPDATE:

Sorry i wasn't very clear. When i say back arrow, i am referring to the arrow in the app bar not the device back button. The arrow in app bar appears when we set the parent of an activity in the manifest file. Thanks!

Yes it is not called because Up button already having the information of Parent Activity statically. So when you press it should go only that Parent Activity.

For example You have three Activities like A, B, C.

    B parent Activity is C

    <activity android:name=".SecondActivity"   android:parentActivityName=".ThirdActivity"/>

    programmatically you go From  A --> B  

        Intent intent=new Intent(MainActivity.this,SecondActivity.class);
            startActivityForResult(intent,1);

Now you press the Up button(back arrow) of (Second) Activity B then it should not go to any Activity. Because it is confused. Regarding manifest file parent Activity is ThirdActivity C but regarding Stack parent Activity is A.

  1. There is some differences between the Up and Back button.

  2. The Up button is used to navigate within an app based on the hierarchical relationships between screens.

  3. When you press Up button it creates the "New Instance" of the Parent Activity (That is only reason OnActivityResult() method not called here) .

  4. But the Back button used to navigate, in reverse chronological order, through the history of screens the user has recently worked with.( the Previous Instance used here, instead of creating new Instance. That is only the reason OnActivityResult() method called in the MainActivity (Parent)).

  5. The Up button, which ensures the user remains within your app, the Back button can return the user to the Home screen, or even to a different app (because, it navigates based on history).

  6. If you want same behavior of Back button for Up button. you should write like this.

      @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case android.R.id.home: super.onBackPressed(); break; } return true; } 

Override onOptionsItemSelected

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Bundle bundle = new Bundle();
                bundle.putString(FIELD_A, "Extra data");

                Intent mIntent = new Intent();
                mIntent.putExtras(bundle);
                setResult(RESULT_OK, mIntent);
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

If you are talking about tollbar back button, this might help

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //do something you want
                finish();
            }
        });
    }

When your SecondActivity exits then your onActivityResult() method will be called with the given requestCode

Try overriding onBackPressed() in SecondActivity

@Override
public void onBackPressed() {
    Bundle bundle = new Bundle();
    bundle.putString(FIELD_A, "Extra data");//passes extra data to MainActivity 

    Intent mIntent = new Intent();
    mIntent.putExtras(bundle);
    setResult(RESULT_OK, mIntent);
    super.onBackPressed();
}

Use the onBackPressed() override to make code execute when you hit the back button.

@Override
public void onBackPressed()
{
     // code here to show dialog
     super.onBackPressed();  // optional depending on your needs
}

当您执行setResult(RESULT_OK, mIntent) ,您应该完成此活动,否则onActivityResult可能不起作用。

For me, none of the answers here worked. I finally tracked the error to the return statement in onOptionsItemSelected(MenuItem m).

A is parent of B. When going from Activity B to Activity A by pressing the toolbar's back arrow button, A's onActivityResult was not triggered when B's onOptionsItemSelected was called thusly:

    public boolean onOptionsItemSelected(MenuItem mi){  
        Switch(mi.getItemId()){

            case: android.R.id.home:

            Intent intent = new Intent();
            intent.putExtras(returnBundle);
            this.setResult(Main.Magnificent_Result, intent);

            finish();
            break;


    }  
    super.onOptionsItemSelected(mi);

But this DOES trigger the onActivityResult in A:

            public boolean onOptionsItemSelected(MenuItem mi){  
                Switch(mi.getItemId()){

                case R.id.android.home:

                Intent intent = new Intent();
                intent.putExtras(returnBundle);
                this.setResult(Main.Magnificent_Result, intent);

                finish();
                return true;   /// Note return statement 


    }  
    super.onOptionsItemSelected(mi);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM