简体   繁体   中英

startActivityForResult returning null when back navigation to activity sending result returns

I have 3 activities that start in a sequence.With the first activity needing a result from the last activity.

I have it so that if Activity B is started for result (By activity A) then Activity B starts activity C for result. Then once the result is captured at activity C it is finished which calls Activity B's onActivityResult which sets the result and finishes and Activity A's onActivityResult gets the final result.

Activity A starts Activity B for a result which Activity C contains thus the sequence is like so

A->B->C (get result) C->B->A (result retrieved)

This works just fine if everything happens in sequence. However, If I navigate to activity C then press the toolbars back arrow and which leads me to activity B then navigate back to activity C and select the result. The result returned to activity A is null.

A->B->C->B->C(get result) C->B->A (result == null)

Activity A

public class AlertCreationActivity extends AppCombatActivity {

   // OnCreate methods left out to shorten code

    @OnClick(R.id.locationButton)
    public void locationButtonClicked() {
        Intent intent = new Intent(this, StateActivity.class);
        intent.putExtra(StateActivity.IS_STARTED_FOR_RESULT, StateActivity.STARTED_FOR_RESULT);
        startActivityForResult(intent, ALERT_CREATION_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == ALERT_CREATION_REQUEST_CODE && resultCode == RESULT_OK) {
            String title = data.getStringExtra(ALERT_CREATION_REQUEST_DATA);
            if ( ! title.isEmpty()) {
                mLocationButton.setVisibility(View.INVISIBLE);
                mLocationTextView.setText(title);
                mLocationTextView.setVisibility(View.VISIBLE);
            }
        }
    }
}

Activity B

public class StateActivity extends AppCompatActivity {
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AlertCreationActivity.ALERT_CREATION_REQUEST_CODE) {
            Intent intent = new Intent();
            intent.putExtra(AlertCreationActivity.ALERT_CREATION_REQUEST_DATA, data.getStringExtra(AlertCreationActivity.ALERT_CREATION_REQUEST_DATA));
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}

Activity B's adapter is where the next intent happens

public class StateAdapter extends RecyclerView.Adapter<StateAdapter.StateViewHolder>  {

    // Other Adapter methods not shown

    public class StateViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        // Other View Holder methods not shown

        @Override
        public void onClick(View v) {
            State selectedState = getStateBy(mLocationLabel.getText().toString());

            // Save state ID
            LocationStorage storage = new LocationStorage(mActivity);
            storage.setSelectedStateId(selectedState.getId());
            storage.setSelectedStateName(selectedState.getName());

            Intent intent = new Intent(mActivity, SpotActivity.class);
            mActivity.startActivityForResult(intent, AlertCreationActivity.ALERT_CREATION_REQUEST_CODE);               

        }
    }
}

Activity C

public class SpotActivity extends AppCompatActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {

         // Initialize actionbar and recyclerview adapter

         // a click on a recyclerview's list item triggers this method
         adapter.setSurfSpotSelectedListener(new SurfSpotSelectedListener() {
            @Override
            public void onSpotSelected(String spotName) {               
                Intent intent = new Intent();
                intent.putExtra(AlertCreationActivity.ALERT_CREATION_REQUEST_DATA, spotName);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
     }
}

导航回activity C是什么意思?如果不启动C,则使用startActivityForResult启动B。

You should set your result by overriding OnBackPressed(), for example:

    @Override
public void onBackPressed() {
    setResult(111, new Intent().putExtra("result", "from c"));
    super.onBackPressed();
}

For Actionbar, you should overriding onOptionsItemSelected(MenuItem item)

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        //here
        setResult(111, new Intent().putExtra("result", "from c"));
        this.finish();
    default:
        return super.onOptionsItemSelected(item);
    }
}

I ended up solving it by setting the setNavigationOnClickListener for the toolbar in Acitvity B andActivity C and calling onBackPressed() to mimic back navigation. Thanks for all your help Tiny Sunlight and Good Try. Here is my Activity B & C. I hope this helps others.

Activity B

public class StateActivity extends AppCompatActivity {

    @Bind(R.id.toolBar) Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spot_selection);
        ButterKnife.bind(this);

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        // Recyclerview and Adapter left out for brevity

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AlertCreationActivity.ALERT_CREATION_REQUEST_CODE) {
            Intent intent = new Intent();
            intent.putExtra(AlertCreationActivity.ALERT_CREATION_REQUEST_DATA, data.getStringExtra(AlertCreationActivity.ALERT_CREATION_REQUEST_DATA));
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}

Activity C

public class SpotActivity extends AppCompatActivity {

    @Bind(R.id.toolBar) Toolbar mToolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spot_selection);
        ButterKnife.bind(this);

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });     

    // Setting up Recyclerview and Adapter omitted

        adapter.setSurfSpotSelectedListener(new SurfSpotSelectedListener() {
            @Override
            public void onSpotSelected(String spotName) {
                Intent intent = new Intent();
                intent.putExtra(AlertCreationActivity.ALERT_CREATION_REQUEST_DATA, spotName);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}

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