Pass an object to a new activity - android

In Xamarin, how can I pass a user defined object (a Class that I have written) to a different activity?
The object I wish to pass is an item in a List called:
_mapLocationList[0]
The item is of type:
MapLocation
Here is my current code:
intent.PutExtra ("MapLocation", _mapLocationList[0]);
Can the above method be used to pass an object? If not, how can I do this?
Thanks in advance

The simplest way of passing objects to an Activity is using Intents. For this, you'll need your class to implement Serializable or Parcelable.
This way, you would put your object in the Intent via the putExtra("myobject", object) method, and then in the other Activity recover it with getSerializableExtra("myobject").
For example:
In the first Activity:
final Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("my_class", your_maplocation_object);
startActivity(intent);
Then in your second Activity, you would do:
final Intent passedIntent = getIntent();
final MapLocation my_class = (MapLocation) passedIntent.getSerializableExtra("my_class");

There is a more simple and very pleasant way : use Googe GSON library.
Serialization in Activity A:
intent.putExtra("mapLocExtra", new Gson().toJson(myMapLocationObject,MapLocation.class);
startActivity(intent);
***Deserialisation in Activity B: *
#Oncreate(...){
String mapLocJson= getIntent().getExtra("mapLocExtra").toString();
MapLocation mylocationObject= new Gson().fromJson(maplocJson,MapLocation.Class);
//do Stuff With mylocationObject
}
More infos : https://code.google.com/p/google-gson/

Related

App crashing on starting another activity with intent and .putExtra

I am trying to open another activity by intent and putExtra.
It used to run fine previously, but now it crashes.
startActivity(new Intent(ForgotPassword.this, OtpVerification.class)
.putExtra("user", user)
.putExtra("otp", verificationId));
finish();
the user here is a class object and am receiving it in another activity
user = getIntent().getParcelableExtra("user");
the app is not even going to another activty and crashes without any error message in logcat.
It only happens if i add .putExtra code
Try adding the putExtra in the following way
Intent intent = new Intent(ForgotPassword.this, OtpVerification.class);
intent.putExtra("user", user);
intent.putExtra("otp", verificationId);
startActivity(intent);
finish();
and then get the intent extras
Intent intent= getIntent();
Bundle extras = intent.getExtras();
if(extras != null)
String data = extras.getString("keyName");
Your user is an Object that you're trying to pass from one activity to another.
So you can create a custom class that implements the Serializable interface.(you must be able to use Parcelable too but i don't have experience with it )
//To pass:
intent.putExtra("User", user);
// To retrieve object in second Activity
getIntent().getSerializableExtra("User");
Point to note: each nested class in your main custom class must have implemented the Serializable interface
eg:
class MainClass implements Serializable {
public MainClass() {}
public static class ChildClass implements Serializable {
public ChildClass() {}
}
}
Ref:How to pass an object from one activity to another on Android
Thank you for your suggestions, the actual problem I found was that my user object had a variable called image (String type) and had a value of length about 1,000,000 (I was storing string encoded image using base64 in it). Once i decreased the size to thousands , it worked. I don't know why the app cannot send such large data across activities.

Passing a reference of an object to activity

I have a reference to an anonymous class object that I want to pass to an activity. It is a kind of reference to a callback fn which gets called based on an action on activity i.e. click of button. But, I dont know a way to do so as activities can not be instantiated directly (done only through startActivity) and using intents will pass my object by value not by reference. So, I need tips on a good solution. I want to avoid statics.
Short Answer: No You cannot pass objects as references to activities.
Try this
CustomListing currentListing = new CustomListing();
Intent i = new Intent();
Bundle b = new Bundle();
b.putParcelable(Constants.CUSTOM_LISTING, currentListing);
i.putExtras(b);
i.setClass(this, SearchDetailsActivity.class);
startActivity(i);
Afterwards to call
Bundle b = this.getIntent().getExtras();
if (b != null)
mCurrentListing = b.getParcelable(Constants.CUSTOM_LISTING);
Here is the answer :
Create a public static method in the Activity to which you want to pass the reference of an object.
public static void openActivity(Activity activity, Class object){
YourNextActivity.object = object;
Intent intent = new Intent(activity, YourNextActivity.class);
activity.startActivity(intent);
}
Call this method from current Activity :
YourNextActivity.openActivity(this, classObject);

Android: How to pass MULTIPLE ArrayList<String> values and one ArrayList<LatLng> from one activity to another in a bundle? Not working

I have several List<String> variables to pass from Splash to the Main activity:
1) I read somewhere that I can pass them as ArrayList<String> from Splash to Main, and it works... i.e.
I can receive only the first ArrayList<String> variable. In my bundle below, I am not able to receive the second ArrayList<String>. (array_list2) Why?
2) How to pass ArrayList<LatLng> from one activity to another
First Activity:
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
Bundle b=new Bundle();
b.putStringArrayList("array_list1",(ArrayList<String>)Names);
b.putStringArrayList("array_list2",(ArrayList<String>)City);
b.putStringArrayList("array_list3",(ArrayList<String>)Country);
b.putStringArrayList("array_list4",(ArrayList<String>)Code);
b.putStringArrayList("array_list5",(ArrayList<LatLng>)coordinates); //ERROR in this line, type mismatch!
intent.putExtras(b);
startActivity(intent);
Second Activity:
Bundle b = getIntent().getExtras();
if (b != null) {
testList1 = b.getStringArrayList("array_list1");
testList2 = b.getStringArrayList("array_list2"); //THIS gives the same arraylist as testList1 and it is incorrect!
Log.e("TESTLIST1",testList1.toString()); //just using Log.e to view o/p as test
Log.e("TESTLIST2",testList1.toString());
Please answer both my questions. None of the other topics helped me, and I spent over 2 hours on this.
Thank you.
putStringArrayList() will not support for ArrayList .
use
putParcelableArrayList();
instead of
putStingArrayList();
or else you can use direct method of intent.
Change your code somethng like this:
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
Bundle b=new Bundle();
b.putStringArrayList("array_list1",(ArrayList<String>)Names);
b.putStringArrayList("array_list2",(ArrayList<String>)City);
b.putStringArrayList("array_list3",(ArrayList<String>)Country);
b.putStringArrayList("array_list4",(ArrayList<String>)Code);
If you want to pass through bundle object use below line of code.
b.putParcelableArrayList("array_list5",(ArrayList<LatLng>)coordinates);
Otherwise pass through intent object.
intent.putParcelableArrayListExtra("array_list5",coordinates);
intent.putExtras(b);
startActivity(intent);
Because LatLng class implements Parcelable interface, so instead of using Bundle. putStringArrayList use Bundle.putParcelableArrayList to send ArrayList which contains class object which is implementing Parcelable interface. Use
b.putParcelableArrayList("array_list5",coordinates);
How to use parcelable with List or ArrayList - What is
the syntax?
Do it as:
ArrayList<String> arrCoordinates = new ArrayList<>(coordinates.size());
arrCoordinates.addAll(coordinates);
b.putParcelableArrayList("array_list5",arrCoordinates);
and get array_list5 as from Bundle
ArrayList<LatLng> coordinates = b.getParcelableArrayList("array_list5");

How to pass an array list from one activity to another without starting it

I have an activity which has an array list
ArrayList<String> array = new ArrayList<String>();
i want this array list to be passed to another activity when a Save button is clicked, but i don't want that activity to start...
Usually this code helps in starting an activity
public void onClick(View v) {
if (v==Save)
{
Bundle bundle = new Bundle();
bundle.putStringArrayList("DONE", activeURL);
Intent myIntent = new Intent(Reader2.this, Aggregator.class);
myIntent.putExtra("reader2", activeURL);
startActivity(intent);
}
}
but i just want to pass the array and start another activity.
Can you please help me ?
Thanks in advance.
You can declare you ArrayList as a static one like this,
public static ArrayList<String> array = new ArrayList<String>();
By doing this you can access your ArrayList from anywhere by
activity_name.array;
where activity_name is the activity or class in which you declare the static ArrayList
you can pass an intent to already running activity.. follow this http://www.helloandroid.com/tutorials/communicating-between-running-activities for that
in the intent you can add an extra like this
Intent contactsIntent = new Intent(getApplicationContext(),
ContactCards.class);
contactsIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
//Bundle containing the serialized list
Bundle extraContacts = new Bundle();
//Putting the array list templist is the array list here
extraContacts.putSerializable("CONTACT_KEY", tempList);
extraContacts.putString("CALL_STRING", CALL_STRING);
contactsIntent.putExtras(extraContacts);
contactsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(contactsIntent);
Based on the fact that you mention a 'Save' button, I think you would rather save this data to SharedPreferences or an SQLiteDatabase.
I am unsure of what it would mean to 'save' some data to another Activity and not start it.
With your data in a persisted state, you should be able to access it from any one of your other Activity's, which is what is sounds like you are after.
use 1st activity
Intent i=new Intent(ArraylistpassActivity.this,second.class);
i.putStringArrayListExtra("key",arl);startActivity(i);
2nd activity:
arl=bundle.getStringArrayList("key");

how to transfer data from one child activity to another using startChildActivity?

this is my class name
public class Register extends TabGroupActivity
I am calling 2nd activity through
startChildActivity("Register", new Intent(Register.this,RegisterForm.class));
can any one help me how to transfer some data through this method
Intent i = new Intent(Register.this,RegisterForm.class);
i.putExtra("name", yourdata);//i assume you are adding some string data
startChildActivity("Register", i);
//in RegisterForm.class
Intent i = RegisterForm.this.getIntent();
i.getStringExtra("name", "default Value you want");
You have to implement an interface called Parecelable and write your object inside a Parcel so that you can transport it through the Intent
This tutorial will teach you how to do that
http://www.codexperience.co.za/post/passing-an-object-between-activities-using-an-intent

Categories

Resources