当返回导航到活动发送结果返回时,startActivityForResult返回null

时间:2015-11-07 16:06:32

标签: java android android-intent android-activity

我有3个以序列开头的活动。第一个活动需要上一个活动的结果。

我拥有它,以便如果为结果启动活动B(按活动A),则活动B为结果启动活动C.然后,一旦在活动C中捕获结果,它就会完成,它调用活动B的onActivityResult,它设置结果并完成,活动A的onActivityResult获得最终结果。

活动A为活动C包含的结果启动活动B,因此序列就像这样

A-> B-> C(得到结果)C-> B-> A(检索结果)

如果一切按顺序发生,这样就可以了。但是,如果我导航到活动C然后按工具栏后退箭头,这会将我引导至活动B,然后导航回活动C并选择结果。返回到活动A的结果为空。

A-> B-> C-> B-> C(得到结果)C-> B-> A(结果==无效)

活动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);
            }
        }
    }
}

活动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();
        }
    }
}

活动B的适配器是下一个意图发生的地方

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);               

        }
    }
}

活动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();
            }
        });
     }
}

3 个答案:

答案 0 :(得分:0)

导航回activity C意味着什么?如果你不开始C startActivityForResult,你就不会得到B的结果。

答案 1 :(得分:0)

您应该通过覆盖OnBackPressed()来设置结果,例如:

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

对于Actionbar,您应该覆盖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);
    }
}

答案 2 :(得分:0)

我最终通过为Acitvity B和Activity C中的工具栏设置setNavigationOnClickListener并调用onBackPressed()来模仿后退导航来解决它。感谢您的帮助Tiny Sunlight和Good Try。这是我的活动B&amp; C.我希望这有助于其他人。

活动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();
        }
    }
}

活动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();
            }
        });
    }
}