How to transfer data between two activities

What is this question about

There is more than one way to transfer data. In addition to the common intent, name the common way in development. The more complete and deeper the familiar way, the better.

How to answer

  1. Intents that can be used to communicate between four components
  2. Registered broadcast receivers can receive broadcasts sent by other activities, and broadcasts can also carry data
  3. Activities need to communicate across processes between multiple apps, and contentProvider can be easily implemented
  4. The SharedPreferences preference, which is also file storage internally
  5. Create a local file for reading and writing
  6. Leverage the lightweight database SQLite
  7. File storage communication methods have multithreaded asynchronous read and write problems, including SharedPreferences, so especially sensitive data caution.

What is Context? How many contexts does an application have

What is this question about

Context, if you’ve learned Android, you know it, but you don’t know a lot about Context systematization. If you can answer this question well, you have your own understanding of the Android kernel.

Subdivided down the knowledge point

  1. The concept of context
  2. Context’s class inheritance structure

How to answer

1. It describes information about an application environment, the context. This class is an abstract class. Android provides a ContextIml for this abstract class. 3. Access to application resources and classes, including application-level operations such as launching an Activity, sending a broadcast, receiving intEnts, messages, and so on. 4. Number of application contexts = Number of Activities + Number of Services +1 Application


How to transfer values between activities, and between activities and services? Can you transfer diagrams?

What is this question about

This question examines how data transfer works in a common development scenario.

Subdivided down the knowledge point

Case description:

  1. The Activity between
  2. Between activities and Services

How to answer

  1. Passing values between activities: startActivity, which is passed through various putExtra methods of Intent objects. In the second Activity object, we use the getIntent() method to get the Intent object that jumps to the Activity, and then use the various getXXExtra methods of the Intent object to get our passed value.

  2. Pass values between activities and services:

    StartService (intent), intent.putstringextra () Intent.getstring (); intent.getString(); intent.getString(); intent.getString(); intent.getString()

    Here we need to register the service in the Mainfeist file

    Public int onStartCommand(Intent Intent, int flags, int startId) retrieves the value from the activity. Intent.getextra () gets the bundle object from which it can take a value.

    An activity can also use bindService(intent, conn,BIND_AUTO_CREATE); The public IBinder onBind(Intent Intent) method of the service obtains the intent.


What data types an Intent can deliver

What is this question about

Consider your proficiency with intents. What values can they deliver, and when intents are appropriate and inappropriate

Subdivided down the knowledge point

  1. The Intent API speaks for itself, supporting basic data types, supporting arrays, and implementing objects that serialize SerialZilable and Parcelable.

  2. Intents are a universal way to transfer data between Android components, but they are not universal

How to answer

  1. Simple data types
  2. An array of
  3. Collections, lists, maps
  4. Object, which can be converted to a JSON string or serialized with Java’s Serializable or Android’s Parcelable
  5. Bitmaps can also be passed, which itself implements Parcelable.
  6. Instead of using intents for multi-page transmission, use a global data warehouse, such as the Application class, which is the ancestor of the context and can be accessed anywhere in the app to retrieve the data in it.