[英]Managing data between three activities and going back to first
In my Activity1, I go to Activity 2 like this: 在我的Activity1中,我像这样转到Activity 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;
}
});
In my Activity2
, I then do a process with the camera and go to Activity3
with a list of strings (this all works, the problem is with the next activity). 然后,在我的
Activity2
,我对相机进行处理,并通过字符串列表转到Activity3
(这一切正常,问题出在下一个活动)。
My 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();
}
});
}
}
}
From my Activity3, I want to go back to Activity1 and in my activity1 for this code to run: 我想从我的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;
}
}
But at the moment, in my Activity3
, when I click on the specific TextView
and it calls finish() it goes back to Activity2
. 但是此刻,在我的
Activity3
,当我单击特定的TextView
并调用finish()时,它返回到Activity2
。 Is there a way to override this so that it goes back to Activity1
instead as I need to use the searchStr
method in OnActivityResult
. 有没有一种方法可以覆盖它,以便它返回到
Activity1
因为我需要在OnActivityResult
使用searchStr
方法。
From Android Developer documentation. 来自Android Developer文档。
FLAG_ACTIVITY_FORWARD_RESULT
FLAG_ACTIVITY_FORWARD_RESULT
public static final int FLAG_ACTIVITY_FORWARD_RESULT
If set and this intent is being used to launch a new activity from an existing one, then the reply target of the existing activity will be transferred to the new activity.
如果已设置并且此意图用于从现有活动启动新活动,则现有活动的答复目标将转移到新活动。 This way, the new activity can call Activity.setResult(int) and have that result sent back to the reply target of the original activity.
这样,新活动可以调用Activity.setResult(int)并将结果发送回原始活动的回复目标。
All you need to do is 您需要做的就是
FLAG_ACTIVITY_FORWARD_RESULT
flag in the intent which starts Activity3
. Activity3
的意图中设置FLAG_ACTIVITY_FORWARD_RESULT
标志。 Activity2
after start Activity3
Activity2
开始后Activity3
Put all together 放在一起
Activity2 活性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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.