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);
and then retrieve this data by
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;
}
}
Now in your Activity:
//after setContentView
testAppObj = (TestApplication) getApplication();
testAppObj.setTempClass(myTempClassObj);
//retrieve as:
TempClass obj = testAppObj.getTempClass();
Hope this helps.
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 " />