在“活动1”中,我这样进入“活动2”:
materialSearchBar.getMenu().setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch(menuItem.getItemId()){
case R.id.searchCamera:
System.out.println("CAMERA GOT HERE");
Intent intent = new Intent(MainActivity.this,CameraText.class);
startActivityForResult(intent,REQ_CODE_CAMERASEARCH);
break;
}
return false;
}
});
然后,在我的Activity2
中,用相机进行处理,并使用字符串列表转到Activity3
(这一切正常,问题在于下一个活动)。
我的活动3:
public class SelectProductName extends AppCompatActivity {
ArrayList<String> getString = new ArrayList<>();
String my_string;
private final int REQ_CODE_CAMERASEARCH = 500;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_product_name);
getString = getIntent().getStringArrayListExtra("stringList");
final TextView[] myTextViews = new TextView[getString.size()];
LinearLayout ll = findViewById(R.id.layout_select_product_name);
for (int i = 0; i < getString.size();i++){
final TextView text = new TextView(this);
text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
text.setText(getString.get(i));
text.setTextSize(20.0f);
System.out.println(getString.get(i));
ll.addView(text);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent returnIntent = new Intent();
my_string = (String) text.getText();
returnIntent.putExtra("csString",my_string);
System.out.println("its here" + my_string);
setResult(REQ_CODE_CAMERASEARCH,returnIntent);
finish();
}
});
}
}
}
在我的Activity3中,我想回到Activity1并在我的activity1中运行此代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_CAMERASEARCH:
if (resultCode == REQ_CODE_CAMERASEARCH && null != data){
String str = data.getStringExtra("csValue");
startSearch(str);
System.out.println("CAMERA SEARCH GETS HERE");
}
break;
}
}
但是现在,在我的Activity3
中,当我单击特定的TextView
并调用finish()时,它返回到Activity2
。有没有一种方法可以覆盖它,使其返回到Activity1
,因为我需要在searchStr
中使用OnActivityResult
方法。
答案 0 :(得分:0)
摘自Android Developer文档。
FLAG_ACTIVITY_FORWARD_RESULT
public static final int FLAG_ACTIVITY_FORWARD_RESULT
如果已设置,并且此意图已用于从 现有活动,那么现有活动的回复目标将是 转移到新活动。这样,新活动可以调用 Activity.setResult(int)并将结果发送回回复 原始活动的目标。
您需要做的就是
FLAG_ACTIVITY_FORWARD_RESULT
的意图中设置Activity3
标志。Activity2
后完成Activity3
全部放在一起
活动2
Intent intent = new Intent(this, Activity3.class);
// Set FLAG_ACTIVITY_FORWARD_RESULT flag in the intent which starts `Activity3`
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(intent);
// Finish Activity2
finish();