【问题标题】:Transfer data from one Activity to Another Activity Using Intents使用 Intent 将数据从一个 Activity 传输到另一个 Activity
【发布时间】:2011-02-11 10:06:12
【问题描述】:

我希望能够将数据从一项活动传输到另一项活动。如何做到这一点?

【问题讨论】:

标签: android android-intent


【解决方案1】:

通过下面的代码,我们可以在活动之间发送值

在父活动中使用以下代码

Intent myintent=new Intent(Info.this, GraphDiag.class).putExtra("<StringName>", value);
startActivity(myintent);

在子活动中使用以下代码

String s= getIntent().getStringExtra(<StringName>);

【讨论】:

  • 澄清像我这样的新手将儿童活动 String s= getIntent().getStringExtra("StringName"); 工作。
  • 只有dor String类型吗??
【解决方案2】:

您可以通过多种方式访问​​其他类或 Activity 中的变量或对象。

A.数据库

B.共享偏好。

C.对象序列化。

D.可以保存公共数据的类可以命名为 Common Utilities,这取决于您。

E.通过 Intents 和 Parcelable 接口传递数据。

这取决于您的项目需求。

A. 数据库

SQLite 是一个嵌入到 Android 中的开源数据库。 SQLite 支持标准的关系数据库功能,如 SQL 语法、事务和准备好的语句。

教程 -- http://www.vogella.com/articles/AndroidSQLite/article.html

B. 共享偏好

假设您要存储用户名。所以现在会有两个东西一个Key用户名,ValueValue。

如何储存

 // Create object of SharedPreferences.
 SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
 //now get Editor
 SharedPreferences.Editor editor = sharedPref.edit();
 //put your value
 editor.putString("userName", "stackoverlow");

 //commits your edits
 editor.commit();

使用 putString(),putBoolean(),putInt(),putFloat(),putLong() 可以保存所需的数据类型。

如何获取

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String userName = sharedPref.getString("userName", "Not Available");

http://developer.android.com/reference/android/content/SharedPreferences.html

C. 对象序列化

如果我们想要保存对象状态以通过网络发送它,或者您也可以将其用于您的目的,则使用对象序列化。

使用 java bean 并将其存储为他的字段之一,并为此使用 getter 和 setter

JavaBean 是具有属性的 Java 类。考虑到 属性作为私有实例变量。既然他们是私人的,唯一的办法 可以通过类中的方法从类外部访问它们。这 改变属性值的方法称为 setter 方法,而这些方法 检索属性值的方法称为 getter 方法。

public class VariableStorage implements Serializable  {

    private String inString ;

    public String getInString() {
        return inString;
    }

    public void setInString(String inString) {
        this.inString = inString;
    }


}

通过使用在邮件方法中设置变量

VariableStorage variableStorage = new VariableStorage();
variableStorage.setInString(inString);

然后使用对象序列化来序列化这个对象,并在你的其他类中反序列化这个对象。

在序列化中,对象可以表示为一个字节序列,其中包括对象的数据以及有关对象类型和对象中存储的数据类型的信息。

序列化的对象写入文件后,可以从文件中读取并进行反序列化,即表示对象及其数据的类型信息和字节可用于在内存中重新创建对象。

如果你想要这个教程参考这个链接

http://javawithswaranga.blogspot.in/2011/08/serialization-in-java.html

Get variable in other classes

D. CommonUtilities

您可以自己创建一个类,其中可以包含您在项目中经常需要的常用数据。

示例

public class CommonUtilities {

    public static String className = "CommonUtilities";

}

E. 通过 Intent 传递数据

有关此数据传递选项,请参阅本教程。

http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/

【讨论】:

  • 另一种方法是使用可以在整个应用程序中访问的 Singleton 类
  • 谢谢你。它真的很有帮助。我有一个整数值,我想在不同的活动之间共享。在我当前的代码中,我将数据从活动 A 发送到活动 B,然后从活动 B 发送到片段 B。如果片段 B 中的数据发生更改,我将通过接口将更改传递给活动 B。然后当活动 B 完成时,我将数据发送回活动 A,这甚至还没有完成,我有一些未修复的错误!使用 CommonUtilities 类是解决我的问题的一种更好的方法。我一定会切换到它。
【解决方案3】:

当您将数据从一个活动传递到另一个活动时,执行如下方式

在父活动中:

startActivity(new Intent(presentActivity.this, NextActivity.class).putExtra("KEY_StringName",ValueData)); 

或像下面父活动中显示的那样

Intent intent  = new Intent(presentActivity.this,NextActivity.class);     
intent.putExtra("KEY_StringName", name);   
intent.putExtra("KEY_StringName1", name1);   
startActivity(intent);

在子Activity中执行如下图

TextView tv = ((TextView)findViewById(R.id.textViewID))
tv.setText(getIntent().getStringExtra("KEY_StringName"));

或在子 Activity 中执行如下所示的操作

TextView tv = ((TextView)findViewById(R.id.textViewID));
TextView tv1 = ((TextView)findViewById(R.id.textViewID1))

/* Get values from Intent */
Intent intent = getIntent();         
String name  = intent.getStringExtra("KEY_StringName");
String name1  = intent.getStringExtra("KEY_StringName1");

tv.setText(name);
tv.setText(name1);

【讨论】:

    【解决方案4】:

    在 android 中将数据从一个活动传递到另一个活动

    Intent intent = new Intent(context, YourActivityClass.class);
    intent.putExtra(KEY, <your value here>);
    
    startActivity(intent);
    

    从 android 活动中检索捆绑数据

    Intent intent = getIntent();
      if (intent!=null) {
      String stringData= intent.getStringExtra(KEY);
      int numberData = intent.getIntExtra(KEY, defaultValue);
      boolean booleanData = intent.getBooleanExtra(KEY, defaultValue);
      char charData = intent.getCharExtra(KEY, defaultValue);   }
    

    【讨论】:

      【解决方案5】:

      希望你能从这里找到答案Send Data to Another Activity - Simple Android Login

      【讨论】:

      • 虽然这在理论上可以回答这个问题,it would be preferable 在此处包含答案的基本部分,并提供链接以供参考。
      【解决方案6】:

      你只需要在调用你的意图时发送额外的东西

      像这样:

      Intent 意图 = new Intent(getApplicationContext(), SecondActivity.class); intent.putExtra("变量名","你要传递的值");开始活动(意图);

      现在在 SecondActivity 的 OnCreate 方法上,您可以像这样获取额外内容

      如果你发送的值是“long”

      long value = getIntent().getLongExtra("你作为额外发送的变量名", defaultValue(你可以给它任何东西));

      如果你发送的值是一个“字符串”

      String value = getIntent().getStringExtra("您作为额外发送的变量名称");

      如果你发送的值是“布尔值”

      Boolean value = getIntent().getStringExtra("您作为额外发送的变量名称",defaultValue);

      【讨论】:

        【解决方案7】:

        你的目的

        假设您想从活动 A 转到活动 B

        所以我们使用 Intent 来切换活动

        典型代码如下所示 -

        在活动 A [A.class]

        //// Create a New Intent object
        Intent i = new Intent(getApplicationContext(), B.class);
        
        /// add what you want to pass from activity A to Activity B
        i.putExtra("key", "value");
        
        /// start the intent
        startActivity(i);
        

        在活动 B [B.class]

        并从子活动

        中获取数据
        Intent i = getIntent();
        
        if (i!=null) {
           String stringData= i.getStringExtra("key");
        }
        

        【讨论】:

          【解决方案8】:

          这样效果最好:

          通过下面的代码,我们可以在活动之间发送值

          在父活动中使用以下代码(父类/值发送类)

          Intent myintent=new Intent(<PARENTCLASSNAMEHERE>.this,<TARGETCLASSNAMEHERE>.class).putExtra("<StringName>", value);
          startActivity(myintent);
          

          在子活动中使用以下代码(目标类/活动)

          String s= getIntent().getStringExtra(<StringName>);
          

          请看这里,“StringName”是目标/子活动捕获的名称,而“value”是变量名称,与父/目标/发送类中的相同。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-08-14
            • 2017-04-05
            • 2015-10-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多