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

标签 java android android-intent android-activity

我正在尝试将用户输入值从第四个 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/

相关文章:

java - 如何将特定线程快速处理为多线程java?

android - 自定义 android 微调器

Android解码并保存从 Intent 返回的音频流

Android M startActivity 电池优化

java - 如何将两个 map 添加到列表中

java - 将生成的类打包到 jar 中并将其添加到构建类路径中

java - 为什么 executeUpdate() 函数不起作用?给出在普通项目而不是基于 maven 的项目中解决的步骤

android - 解析 : Push Notifications "deviceToken" undefined

android - 如何在 Android 中隐藏进度条?

java - 两个 Activity 之间的 Intent 出现 NullPointerException