在主要活动中你应该有这个:
public static final int REQUEST_CODE = 1;
Button button = (Button) findViewById(R.id.your_button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// actions that will happen when the button is pressed:
Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
});
在第二个活动中,您应该从点击的 RecyclerView 传递列表项的位置等结果,像这样:
ExampleClickAdapter clickAdapter = new ExampleClickAdapter(yourObjects);
clickAdapter.setOnEntryClickListener(new ExampleClickAdapter.OnEntryClickListener() {
@Override
public void onEntryClick(View view, int position) {
Intent intent = new Intent();
intent.putExtra("pos", position);
setResult(Activity.RESULT_OK, intent);
finish();
}
});
recyclerView.setAdapter(clickAdapter);
在主要活动中,您应该有一个获取结果的方法,这是您的方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
int result = data.getIntExtra("pos");
// do something with the result
} else if (resultCode == Activity.RESULT_CANCELED) {
// some stuff that will happen if there's no result
}
}
}