How to pass information between Activities

2019-07-26 21:39发布

问题:

I have asked this question a few times on here already, but still am not confident enough to start throwing things into my code because I'm afraid I'll ruin the functionality it already has.

What I want to do is have a Bluetooth connection with a device that I can send/receive data to/from, and be able to access it from several different Activities. I know Intents can be used, but can anyone try to explain how intents work?

I don't understand how if I start with an Activity, how I can build an object within that Activity, then end it, and still have the object. When I call the intent in the following Activity, how do I access it?

I can not find example code of an Activity with a custom object that is passed through an intent, opened in another Activity, then passed again through another Activity, and so on. All I can find is things for string's, int's and it seems that those methods are hardcoded for the specific objects.

Any help would be greatly appreciated, thanks.

回答1:

there is a good reason you don't find a good example of transfering complicate objects: you don't suppose to that a lot. it's possible to transfer with intent extra any object, with declare the class you want to transfer as implements Serializable, or Parcalable.

but - this is not recomended doing so, unless you have good reason. there are unlimited ways to create "cross activities" objects (databases, singeltones, services..), and passing complicated objects in intent is heavy operation. also it makes terrible performances doing so.

anyway - if you want anyway doing so, that's the way:

this is how to pass serlizable object:

Intent intent =  new Intent();
    intent.putExtra(name, someInstanceOfClassWhichImplementsSerializableInterface);

this is how to acheive it on the other activity:

getIntent().getSerializableExtra(name);

about the question that you don't understand how the object still lives after you finish the activity - finish() activity is not triggering distractor!!!, but do triggers to onDestoy() callback. the system decides when it's the right time call the activities distracor , and even if it was distractor - it's not preventing from the Intent object to stay alive - because nobody said your activity is the only one holding reference to it. the intent reference held by the system for the perpose of passing it to the new activity which opened with it.



回答2:

If you want to pass a custom object from an Activity to another one, your object have to implement Parcelable interface.

Here you can find an example of an object which implements Parcelable. Once you have an instance of this object you can add it to an intent and pass it to other activity.

Let's say you have 2 activities: A and B. If you want to send a custom object from A to B try put your parceable object in an intent and get it back in Activity B.

// Activity A
Notebook myNotebook = new Notebook(); // the "Parcelable" object
Intent intent = new Intent(A.this, B.class);
intent.putExtra("object", myNotebook);
startActivityForResult(intent);

// Activity B
// in onCreate method
Notebook notebook = intent.getParcelableExtra("object");


回答3:

Using intent you can send serialized object which is like converting you object into byte stream. If you want to send your object as it is you can use handlers. e.g.

  1. create Bluetooth connection using service(android service)
  2. define handler in you activity which will be static object
  3. from service to send the object add to msg.obj send that msg to handler