简体   繁体   English

将JSONObject意图转移到另一个活动

[英]Intent JSONObject to another activity

I want to pass JSONObject to another activity in the else section in he below code so i can use profile details in the other activity, 我想将JSONObject传递给下面代码中else部分的另一个活动,以便我可以在其他活动中使用配置文件详细信息,

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();
    }
}

to pass a JSONObject you can use a String . 要传递JSONObject ,可以使用String For instance: 例如:

 Intent intent = new Intent(this, RegisterActivity.class);
 intent.putExtra("profile", profile.toString());
 intent.putExtra("result", result.toString());
 this.startActivity(intent);

toString() implementation of the JSONObject , class rncodes the object as a compact JSON string. JSONObject toString()实现,该类将对象编码为紧凑的JSON字符串。 In your RegisterActivity , to retrieve the String: 在您的RegisterActivity ,检索字符串:

 String profile = getIntent().getStringExtra("profile");
 JSONObject profileJSON = new JSONObject(profile)

You can not pass objects easily to another activity, if the object is not parcelable. 如果对象不可包裹,则不能轻易将其传递给另一个活动。 To achieve this, you can create a class. 为此,您可以创建一个类。 For example name it, RunTimeData. 例如,将其命名为RunTimeData。

In this class create a static JSONObject. 在此类中,创建一个静态JSONObject。

public class RunTimeData{
    public static JSONObject object;
}

So whenever you want to store the data, you can store it as follows. 因此,无论何时要存储数据,都可以按以下方式存储。

RunTimeData.object=result;

When you want to use this, in another activity use, 当您想使用此功能时,在另一项活动中使用,

JSONObject result=RunTimeData.object.

And finally when you are sure that you are not going to use this value anymore in the program, null it. 最后,当您确定不再在程序中使用此值时,请将其为空。

RunTimeData.object=null;

Blackbelt is right, but another valid solution is to use a simple object implementing Parcelable Blackbelt是正确的,但另一个有效的解决方案是使用实现Parcelable的简单对象

eg 例如

MyObject obj = new MyObject();
obj.setTitle(myJsonObject.getJSONObject("title").opt("val").toString());
...
intent.putExtra("someCoolTAG", obj);

You can use global data 您可以使用全局数据

public class GlobalData extends Application{
    JSONObject jsonObj;
}

else{
    ((GlobalData)getApplication).jsonObj = myJsonObj;
    startActivity(new Intent(...));
}

You also need to declare in manifest in application tag android:name=".GlobalData" 您还需要在清单中的应用程序标签android:name =“。GlobalData”中声明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM