How to send an object from one Android Activity to

2018-12-31 00:13发布

How can I pass an object of a custom type from one Activity to another using the putExtra() method of the class Intent?

30条回答
爱死公子算了
2楼-- · 2018-12-31 00:44

I struggled with the same problem. I solved it by using a static class, storing any data I want in a HashMap. On top I use an extension of the standard Activity class where I have overriden the methods onCreate an onDestroy to do the data transport and data clearing hidden. Some ridiculous settings have to be changed e.g. orientation-handling.

Annotation: Not providing general objects to be passed to another Activity is pain in the ass. It's like shooting oneself in the knee and hoping to win a 100 metres. "Parcable" is not a sufficient substitute. It makes me laugh... I don't want to implement this interface to my technology-free API, as less I want to introduce a new Layer... How could it be, that we are in mobile programming so far away from modern paradigm...

查看更多
孤独寂梦人
3楼-- · 2018-12-31 00:45

Short answer for fast need

1. Implement your Class to Serializable.

If you have any inner Classes don't forget to implement them to Serializable too!!

public class SportsData implements  Serializable
public class Sport implements  Serializable

List<Sport> clickedObj;

2. Put your object into Intent

 Intent intent = new Intent(SportsAct.this, SportSubAct.class);
            intent.putExtra("sport", clickedObj);
            startActivity(intent);

3. And receive your object in the other Activity Class

Intent intent = getIntent();
    Sport cust = (Sport) intent.getSerializableExtra("sport");
查看更多
还给你的自由
4楼-- · 2018-12-31 00:45

If you are not very particular about using the putExtra feature and just want to launch another activity with objects, you can check out the GNLauncher (https://github.com/noxiouswinter/gnlib_android/wiki#gnlauncher) library I wrote in an attempt to make this process more straight forward.

GNLauncher makes sending objects/data to an Activity from another Activity etc as easy as calling a function in the Activity with the required data as parameters. It introduces type safety and removes all the hassles of having to serialize, attaching to the intent using string keys and undoing the same at the other end.

查看更多
梦该遗忘
5楼-- · 2018-12-31 00:46

the most easiest solution i found is.. to create a class with static data members with getters setters.

set from one activity and get from another activity that object.

activity A

mytestclass.staticfunctionSet("","",""..etc.);

activity b

mytestclass obj= mytestclass.staticfunctionGet();
查看更多
春风洒进眼中
6楼-- · 2018-12-31 00:46

The simplest would be to just use the following where the item is a string:

intent.putextra("selected_item",item)

For receiving:

String name = data.getStringExtra("selected_item");
查看更多
看淡一切
7楼-- · 2018-12-31 00:48

you can use putExtra(Serializable..) and getSerializableExtra() methods to pass and retrieve objects of your class type; you will have to mark your class Serializable and make sure that all your member variables are serializable too...

查看更多
登录 后发表回答