Skip to content
CMO & CTO
CMO & CTO

Closing the Bridge Between Marketing and Technology, By Luis Fernandez

  • Digital Experience
    • Experience Strategy
    • Experience-Driven Commerce
    • Multi-Channel Experience
    • Personalization & Targeting
    • SEO & Performance
    • User Journey & Behavior
  • Marketing Technologies
    • Analytics & Measurement
    • Content Management Systems
    • Customer Data Platforms
    • Digital Asset Management
    • Marketing Automation
    • MarTech Stack & Strategy
    • Technology Buying & ROI
  • Software Engineering
    • Software Engineering
    • Software Architecture
    • General Software
    • Development Practices
    • Productivity & Workflow
    • Code
    • Engineering Management
    • Business of Software
    • Code
    • Digital Transformation
    • Systems Thinking
    • Technical Implementation
  • About
CMO & CTO

Closing the Bridge Between Marketing and Technology, By Luis Fernandez

First Steps with Android: Activities, Intents, Lifecycles

Posted on November 5, 2013 By Luis Fernandez
\n
\n\n\n

KitKat just landed and the Nexus 5 buzz is real. Android Studio is in preview while many of us still ship apps with Eclipse ADT. Tools aside, the first wall new Android folks hit is always the same: what is an Activity, what is an Intent, and why does the lifecycle seem to have more steps than a salsa class. Let�s make this simple and practical so you can ship your first screen without getting lost in docs.

\n\n\n\n

Problem framing

\n\n\n\n

Every Android app is a set of screens. Each screen is an Activity. Activities talk to each other with Intents. While the user taps around or rotates the phone, Android calls lifecycle methods like onCreate, onStart, onResume, onPause, onStop, and onDestroy. If you understand those three ideas you can build a clean flow and avoid weird bugs like vanishing data on rotation or a back button that feels haunted.

\n\n\n\n

Three case walkthrough

\n\n\n\n

Case 1: Go from a list to a detail with an explicit Intent

\n\n\n\n

You have MainActivity with a list and a DetailActivity. You know exactly where you want to go, so use an explicit Intent. First, declare the second screen in the manifest:

\n\n\n\n
<application ...>\n    <activity android:name=".DetailActivity" />\n</application>
\n\n\n\n

Now launch it and pass data with extras:

\n\n\n\n
// MainActivity.java\nIntent i = new Intent(MainActivity.this, DetailActivity.class);\ni.putExtra("post_id", 42);\nstartActivity(i);
\n\n\n\n

Receive it in the target:

\n\n\n\n
// DetailActivity.java\n@Override\nprotected void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.activity_detail);\n\n    int postId = getIntent().getIntExtra("post_id", -1);\n}
\n\n\n\n

Want a result back, say a picked item id. Start with startActivityForResult and handle onActivityResult.

\n\n\n\n
// MainActivity.java\nstartActivityForResult(i, 1001);\n\n@Override\nprotected void onActivityResult(int requestCode, int resultCode, Intent data) {\n    if (requestCode == 1001 && resultCode == RESULT_OK) {\n        int picked = data.getIntExtra("picked_id", -1);\n    }\n}
\n\n\n\n

Case 2: Share or open content with an implicit Intent

\n\n\n\n

When you do not care which app handles the action, use an implicit Intent. Android picks a match using the intent filters other apps declare.

\n\n\n\n
// Share text\nIntent share = new Intent(Intent.ACTION_SEND);\nshare.setType("text/plain");\nshare.putExtra(Intent.EXTRA_TEXT, "Hello from my Android Activity");\nstartActivity(Intent.createChooser(share, "Share with"));\n\n// Open a map\nUri place = Uri.parse("geo:0,0?q=Buenos Aires");\nstartActivity(new Intent(Intent.ACTION_VIEW, place));\n\n// Dial a number\nUri phone = Uri.parse("tel:123456789");\nstartActivity(new Intent(Intent.ACTION_DIAL, phone));
\n\n\n\n

To make your Activity discoverable by others, add an intent filter. Now your screen can open http links from a browser.

\n\n\n\n
<activity android:name=".DetailActivity">\n    <intent-filter>\n        <action android:name="android.intent.action.VIEW" />\n        <category android:name="android.intent.category.DEFAULT" />\n        <category android:name="android.intent.category.BROWSABLE" />\n        <data android:scheme="http" android:host="example.com" android:pathPrefix="/post"/>\n    </intent-filter>\n</activity>
\n\n\n\n

Case 3: Survive rotation and the back stack with lifecycle calls

\n\n\n\n

Rotate the phone and Android destroys and recreates your Activity. If your screen loses the text the user typed, fix it with onSaveInstanceState and read it back in onCreate or onRestoreInstanceState.

\n\n\n\n
private static final String KEY_COUNT = "count";\nprivate int count = 0;\n\n@Override\nprotected void onSaveInstanceState(Bundle out) {\n    super.onSaveInstanceState(out);\n    out.putInt(KEY_COUNT, count);\n}\n\n@Override\nprotected void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.activity_main);\n    if (savedInstanceState != null) {\n        count = savedInstanceState.getInt(KEY_COUNT);\n    }\n}
\n\n\n\n

Watch the back stack and state changes with logs. This will save you hours.

\n\n\n\n
@Override protected void onStart() { super.onStart(); Log.d("MainActivity", "onStart"); }\n@Override protected void onResume(){ super.onResume(); Log.d("MainActivity", "onResume"); }\n@Override protected void onPause() { super.onPause(); Log.d("MainActivity", "onPause"); }\n@Override protected void onStop()  { super.onStop();  Log.d("MainActivity", "onStop");  }
\n\n\n\n

Objections and replies

\n\n\n\n

This lifecycle thing is too much for a tiny app. Small today grows tomorrow. A simple onSaveInstanceState and basic logs keep you safe without extra work.

\n\n\n\n

Intents feel like magic and I do not know who will handle them. Use PackageManager.resolveActivity or wrap with Intent.createChooser. Test on a device and emulator to see choices.

\n\n\n\n

Should I use Fragments instead of multiple Activities. Fragments help with tablets and reusable UI, but the core rules do not change. The Activity lifecycle still runs the show. Start with Activities. Add Fragments when you have a reason.

\n\n\n\n

Do I need Android Studio right now. Studio is getting nicer every month but ADT still ships apps fine. Pick the one you feel faster in. The Activity and Intent code is the same on both.

\n\n\n\n

Action steps

\n\n\n\n

Ready to try this on a fresh project with KitKat or your current target. Do this now.

\n\n\n\n
    \n
  • Create a project with one empty Activity. Add a second Activity in the manifest and wire an explicit Intent with one extra.
  • \n
  • Add a share button that fires an implicit ACTION_SEND. Test the chooser.
  • \n
  • Log lifecycle methods. Press Home, Back, rotate, and watch the order in adb logcat.
  • \n
  • Persist a counter with onSaveInstanceState. Rotate to confirm it sticks.
  • \n
  • Optional: return data with startActivityForResult to get comfortable with results.
  • \n
\n\n\n\n
// Quick logcat tip\nadb logcat ActivityManager:I MyApp:D *:S
\n\n\n\n

If you keep these three ideas close � Activity as a screen, Intent as the messenger, and lifecycle as the rules of the house � you will move faster, your back button will behave, and your users will not lose data after a simple rotation. Ship something small today and build on it.

\n\n\n
\n
General Software Software Engineering

Post navigation

Previous post
Next post
  • Digital Experience (94)
    • Experience Strategy (19)
    • Experience-Driven Commerce (5)
    • Multi-Channel Experience (9)
    • Personalization & Targeting (21)
    • SEO & Performance (10)
  • Marketing Technologies (92)
    • Analytics & Measurement (14)
    • Content Management Systems (45)
    • Customer Data Platforms (4)
    • Digital Asset Management (8)
    • Marketing Automation (6)
    • MarTech Stack & Strategy (10)
    • Technology Buying & ROI (3)
  • Software Engineering (310)
    • Business of Software (20)
    • Code (30)
    • Development Practices (52)
    • Digital Transformation (21)
    • Engineering Management (25)
    • General Software (82)
    • Productivity & Workflow (30)
    • Software Architecture (85)
    • Technical Implementation (23)
  • 2025 (12)
  • 2024 (8)
  • 2023 (18)
  • 2022 (13)
  • 2021 (3)
  • 2020 (8)
  • 2019 (8)
  • 2018 (23)
  • 2017 (17)
  • 2016 (40)
  • 2015 (37)
  • 2014 (25)
  • 2013 (28)
  • 2012 (24)
  • 2011 (30)
  • 2010 (42)
  • 2009 (25)
  • 2008 (13)
  • 2007 (33)
  • 2006 (26)

Ab Testing Adobe Adobe Analytics Adobe Target AEM agile-methodologies Analytics architecture-patterns CDP CMS coding-practices content-marketing Content Supply Chain Conversion Optimization Core Web Vitals customer-education Customer Data Platform Customer Experience Customer Journey DAM Data Layer Data Unification documentation DXP Individualization java Martech metrics mobile-development Mobile First Multichannel Omnichannel Personalization product-strategy project-management Responsive Design Search Engine Optimization Segmentation seo spring Targeting Tracking user-experience User Journey web-development

©2025 CMO & CTO | WordPress Theme by SuperbThemes