如何使用1意图活动之间的运输? [英] How to use 1 Intent to transport between activities?

查看:122
本文介绍了如何使用1意图活动之间的运输?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Xamarin开发Android程序。
我的应用程序有哪些,他们通过使用例如该code数据传递给对方许多不同的活动:

I'm using Xamarin to develop android programs. My app has many different activities which they pass data to each other by using for example this code:

Intent myIntent = new Intent(this, typeof(ViewAccountActivity));
myIntent.PutExtra("loginedcustomer", JsonConvert.SerializeObject(loginedCustomer));
StartActivity(myIntent);

然后

loginedCustomer = JsonConvert.DeserializeObject<Customer>(Intent.GetStringExtra("loginedcustomer"));

现在我的问题是,对于每一个过渡到另一个活动要我做的意图,并把数据并启动它,这意味着我必须检索数据,然后再次把它和开始我的新意图。
有没有更简单的方式做到这一点?
像使用一个意图,只是在那里为首的变化?(在构造函数中)
谢谢

now my question is that for every transition to another activity i have to make intent and put the data and start it which means that i have to retrieve the data and then put it again and start my new intent. is there any easier way to do this? like using one intent and only change where it is headed?(in the constructor) thank you

推荐答案

您可以使用应用程序类在许多活动访问同一个对象。

You can use Application class for accessing same object across many activities.

public class TestApplication extends Application {

    public TempClass tempClass;

    public TestApplication () {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate() { 
        // TODO Auto-generated method stub
        super.onCreate();
    }

    public TempClass getTempClass() {
        return tempClass;
    }

    public void setTempClass(TempClass tempClass) {
        this.tempClass  = tempClass;
    }
}

现在在你的活动:

//after setContentView
testAppObj = (TestApplication) getApplication();
testAppObj.setTempClass(myTempClassObj);

//retrieve as:
TempClass obj = testAppObj.getTempClass();

希望这有助于。

P.S:
您必须注册您的应用程序类在你的清单文件,就像您注册活动:

P.S: You must register your Application class in your manifest file just like you register your activities:

<application
        android:name="com.pkg.test.TestApplication " />

这篇关于如何使用1意图活动之间的运输?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆