gpt4 book ai didi

java - 使用多个 Intent 时如何避免跳过 Activity ?

转载 作者:行者123 更新时间:2023-12-02 05:29:51 25 4
gpt4 key购买 nike

我正在尝试将用户输入值从第四个 Activity 传递到第五个和第六个 Activity 。我已经使用 Intent 来传递值。但是现在当我运行应用程序时,它会在单击按钮时从第四个 Activity 跳转到第六个 Activity ,跳过第五个 Activity 。是因为我同时使用了这两个 Intent 吗?我该如何修改代码以避免这种情况?

第四.java

protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fourth);

final EditText et;
final Button b;

et = (EditText) findViewById(R.id.editText1);
b = (Button) findViewById(R.id.button1);

b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(Fourth.this, Fifth.class);
intent.putExtra("thetext", et.getText().toString());
startActivity(intent);

Intent intentnew = new Intent(Fourth.this, Sixth.class);
intentnew.putExtra("thetext", et.getText().toString());
startActivity(intentnew);
}
}
}

最佳答案

以下是您可以使用的一些选项。

  1. 由于您只想在此时使用 Intent,因此您可以使用 Intent 将第四个 Activity 的数据传递到第五个 Activity。然后再次传递相同的数据从第五个 Activity 到第六个 Activity ,使用第五个 Activity 中的另一个 Intent

所以在你的第四个 Activity 中有这个

Intent intent = new Intent(Fourth.this, Fifth.class);
intent.putExtra("thetext", et.getText().toString());
startActivity(intent);

在你的第五个中,

String text = getIntent().getStringExtra("thetext");
Intent intentnew = new Intent(Fifth.this, Sixth.class);
intentnew.putExtra("theSametext", text);
startActivity(intentnew);

2.您可以将数据保存到SharedPreferences - 使用此功能您可以将信息保存到应用程序的“首选项”文件中。 Refer this question and answer了解如何使用它。

3.将其写入SQLite数据库 - 您可以创建一个新的SQLite表来存储数据并向其中写入新行。这会产生更多的开销,并且只有在同一应用程序中要存储大量数据时才真正有用。您可以 refer this tutorial为此。

4.您还可以创建一个 Singelton 类,它可以是具有可设置的公共(public)属性的静态类。但是,这仅适用于跨多个 Activity 临时创建和保留数据。

因此,如果您想使用 Intent 来实现,则只能使用第一种方法。

关于java - 使用多个 Intent 时如何避免跳过 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25666842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com