使用Intent将数据从一个Activity传输到另一个Activity

use*_*910 21 android android-intent

我希望能够将数据从一个活动转移到另一个活动.如何才能做到这一点?

Pin*_*nki 29

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

在父活动中使用以下代码

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

在儿童活动中使用以下代码

String s= getIntent().getStringExtra(<StringName>);
Run Code Online (Sandbox Code Playgroud)


Nik*_*wal 14

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

A.数据库

B.共享偏好.

C.对象序列化.

D.可以保存公共数据的类可以命名为依赖于您的Common Utilities.

E.通过Intents和Parcelable Interface传递数据.

这取决于您的项目需求.

A. 数据库

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

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

B. 共享偏好

假设您要存储用户名.因此,现在将有两个关键用户名,即价值值.

如何存储

 // 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();
Run Code Online (Sandbox Code Playgroud)

使用putString(),putBoolean(),putInt(),putFloat(),putLong()可以保存所需的dtatype.

如何获取

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String userName = sharedPref.getString("userName", "Not Available");
Run Code Online (Sandbox Code Playgroud)

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

C. 对象序列化

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

使用java bean并将其作为其中一个字段存储在其中,并使用getter和setter

JavaBeans是具有属性的Java类.将属性视为私有实例变量.由于它们是私有的,因此可以通过类中的方法从类外部访问它们.更改属性值的方法称为setter方法,而检索属性值的方法称为getter方法.

public class VariableStorage implements Serializable  {

    private String inString ;

    public String getInString() {
        return inString;
    }

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


}
Run Code Online (Sandbox Code Playgroud)

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

VariableStorage variableStorage = new VariableStorage();
variableStorage.setInString(inString);
Run Code Online (Sandbox Code Playgroud)

然后使用对象序列化来序列化此对象,并在其他类中反序列化此对象.

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

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

如果您想要这方面的教程,请参阅此链接

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

在其他类中获取变量

D. CommonUtilities

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

样品

public class CommonUtilities {

    public static String className = "CommonUtilities";

}
Run Code Online (Sandbox Code Playgroud)

E. 通过意图传递数据

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

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


Rah*_*dia 10

将数据从一个活动传递到另一个活动时,执行此操作

在父活动中:

startActivity(new Intent(presentActivity.this, NextActivity.class).putExtra("KEY_StringName",ValueData)); 
Run Code Online (Sandbox Code Playgroud)

或者如下面的父活动中所示

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

在子活动中执行如下所示

TextView tv = ((TextView)findViewById(R.id.textViewID))
tv.setText(getIntent().getStringExtra("KEY_StringName"));
Run Code Online (Sandbox Code Playgroud)

或者在子活动中如下所示

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);
Run Code Online (Sandbox Code Playgroud)