Java Intent JSONObject指向另一个活动
我想将JSONObject传递给下面代码中else部分的另一个活动,这样我就可以在其他活动中使用配置文件详细信息Java Intent JSONObject指向另一个活动,java,android,json,Java,Android,Json,我想将JSONObject传递给下面代码中else部分的另一个活动,这样我就可以在其他活动中使用配置文件详细信息 public void redirectToActivity(JSONObject result, JSONObject profile){ try { String status = result.getString("status"); if (status.equals("204")) {
public void redirectToActivity(JSONObject result, JSONObject profile){
try {
String status = result.getString("status");
if (status.equals("204")) {
Intent intent = new Intent(this, ActivityMenu.class);
intent.putExtra("user", "email");
this.startActivity(intent);
}
else {
Intent intent = new Intent(this, RegisterActivity.class);
this.startActivity(intent);
//here I want to pass JSONObject profile to RegisterActivity
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
要传递
JSONObject
,可以使用字符串
。例如:
Intent intent = new Intent(this, RegisterActivity.class);
intent.putExtra("profile", profile.toString());
intent.putExtra("result", result.toString());
this.startActivity(intent);
toString()
实现JSONObject
,类RN将对象编码为压缩JSON字符串。在注册表活动中
,要检索字符串:
String profile = getIntent().getStringExtra("profile");
JSONObject profileJSON = new JSONObject(profile)
如果对象不可包裹,则无法轻松将对象传递给其他活动。要实现这一点,您可以创建一个类。例如,将其命名为RunTimeData 在这个类中创建一个静态JSONObject
public class RunTimeData{
public static JSONObject object;
}
因此,无论何时您想要存储数据,都可以按如下方式存储它
RunTimeData.object=result;
当您要使用此选项时,请在另一个活动中使用
JSONObject result=RunTimeData.object.
最后,当您确定不再在程序中使用此值时,请将其设为null
RunTimeData.object=null;
Blackbelt是正确的,但另一个有效的解决方案是使用简单的对象实现 e、 g
您可以使用全局数据
public class GlobalData extends Application{
JSONObject jsonObj;
}
else{
((GlobalData)getApplication).jsonObj = myJsonObj;
startActivity(new Intent(...));
}
您还需要在应用程序标记的清单中声明
android:name=“.GlobalData”如何从RegisterActivity接收这些配置文件数据?Intent=getIntent();字符串JSONObject=intent.getStringExtra(“JSONObject”);请纠正我,如果我错了,请不要这样做,完全没有必要重写应用程序类,只是为了创建一个全局对象来在活动之间传递一个对象。这是一种低效且极其糟糕的做法。
public class GlobalData extends Application{
JSONObject jsonObj;
}
else{
((GlobalData)getApplication).jsonObj = myJsonObj;
startActivity(new Intent(...));
}