Build likes and share actions for Android social apps
Implementing like and share features in social apps should feel immediate without weakening data consistency. A like needs to identify the person and post involved, survive process death and recover from failed requests. Sharing should pass selected content to another app without exposing private account data or assuming a destination is installed.
On Android, separate these jobs. Keep the interface responsive, store pending actions locally and synchronise them in the background. The system Sharesheet handles destination selection, while Room or SQLite can hold reactions, cached feed items and idempotency keys. SharedPreferences suits small settings, but it is a poor home for a growing social graph.
Australian products need local context. Sydney and Melbourne are strong markets for community, travel and café apps, where posts often support informal meet-ups and neighbourhood recommendations; local apps also compete for attention with Instagram, Facebook and TikTok. Scheduling should account for state public holidays, school holidays and major events such as Australia Day, while regional users may alternate between fixed and mobile coverage, making offline recovery essential.
Store reactions as durable relationships
Treat a like as a relationship between an account and a post. Store accountId, postId and createdAt, then derive the displayed count from those records. A count kept only on the post can be overwritten when two devices react simultaneously, while a composite database key prevents duplicate reactions.
Room can enforce that key locally. Keep display names and post text in their own tables so the reaction record stays compact and queries remain fast.
@Entity(
tableName = "reactions",
primaryKeys = ["accountId", "postId"]
)
data class ReactionEntity(
val accountId: String,
val postId: String,
val createdAt: Long
)
Treat the visible count as a cache. Update it optimistically for fast feedback, then reconcile it with the server response. Give every create or remove request an idempotency key so retries cannot toggle the same reaction twice.
Drive the like button from observable state
Expose reaction state through a ViewModel or another observable state holder. A MaterialButton can show an outlined heart when unliked and a filled heart when selected, but the icon alone must not carry the state. Model the selected state explicitly, update the accessibility label and prevent a rapid second tap from issuing contradictory requests before the first response arrives.
Handle three outcomes explicitly: confirmation, rejection and pending work. Restore pending state from Room after a process restart. If the service is unavailable, let the user keep the reaction queued or remove it; automatic retry suits a like, while explicit failure feedback suits a destructive action.
| Storage pattern | Strength | Limitation | Best fit |
|---|---|---|---|
| SharedPreferences | Simple reads for small values | Poor relational queries and weak recovery for queues | Lightweight flags and small demos |
| Room or SQLite | Transactions, constraints and durable pending actions | Requires a schema and deliberate sync logic | Production local reaction store |
| Server only | Authoritative multi-device state | Poor offline behaviour and ambiguous failed actions | Backend reconciliation, not the sole client store |
Share content through the Android Sharesheet
Share the selected item rather than the app’s whole screen. Build an ACTION_SEND intent with text, a canonical web link or a content URI supplied by a FileProvider. Pass only the permissions the receiver needs and present the result through Intent.createChooser, allowing users to choose Messages, email, social apps or any installed destination.
val shareIntent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "$title\n$canonicalUrl")
}
startActivity(Intent.createChooser(shareIntent, "Share post"))
Create user-facing text that explains exactly what will be shared. A public post can include the app name and canonical URL; a private item needs confirmation and must omit identifiers. Test destinations that return no result, revoke URI access or behave differently across Android versions.
If a share also needs two-way progress updates, the Messenger service example demonstrates how a bound service can exchange messages between an Android service and its client. This suits a custom transfer workflow, but it does not replace ACTION_SEND for ordinary sharing. Keep the paths separate so a failed third-party share never marks publication as failed.
Make syncing reliable and privacy-aware
Use WorkManager for durable reaction and publication uploads. Attach network constraints, retry with exponential backoff and persist operation IDs so foreground work, background work and manual retries cannot duplicate posts. Store timestamps in UTC and render them for each Australian city’s local time zone, including daylight-saving changes.
Follow the Australian Privacy Principles when handling account IDs, location-derived content and sharing history. Collect the least data needed, explain its purpose in plain English and provide deletion controls. Never put access tokens or private object keys inside an outgoing intent; a recipient should not inherit broad access to a shared source file.
Test on representative devices and Australian network conditions, including constrained mobile data in regional areas. Track share selection separately from confirmed delivery because the Sharesheet can report a chosen destination without confirming that the content was published there.
Apply practical release recommendations
A release-ready implementation combines durable local state, observable UI, explicit synchronisation and privacy-aware sharing. Use these checks as the acceptance baseline.
- Make likes idempotent and preserve pending operations across restarts.
- Expose selected state and count updates to accessibility services.
- Share canonical links or scoped file URIs, never private credentials.
- Distinguish a chosen share destination from confirmed publication.
- Store UTC timestamps and format them for Australian local time.
The expected end-to-end result is straightforward: after restarting the app, liking and unliking an item preserves the correct state; sharing through two destinations sends only the intended content; and repeated actions under poor connectivity leave the server, local database and analytics in agreement.