Junior — Middle
What difficulties can be encountered when transferring data between Activities in Android?
sobes.tech AI
Answer from AI
When passing data between Activities in Android, you may encounter several challenges:
- Size limitation: Intent has a limit on the size of data it can pass (usually around 1 MB). Passing large objects or arrays can cause the app to crash.
- Object serialization: To pass complex objects, you need to implement the Parcelable or Serializable interface. Incorrect implementation can lead to errors during transfer.
- Data loss during Activity recreation: If an Activity is recreated (for example, during screen rotation), the passed data may be lost if not saved in a Bundle.
- Security: Transferred data can be intercepted if protected mechanisms are not used, especially when passing between different applications.
Example of passing simple data via Intent:
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("username", "user123");
startActivity(intent);
// In NextActivity
String username = getIntent().getStringExtra("username");