The Very Basics of Android: Starting Activities

When you click on a link on a webpage to navigate to another webpage, what do you need to tell the backend in order for it to fetch the right page for you? First, you have to tell the backend which page you want to see. Second, you have to tell the backend any extra information it might require. You take these two pieces of information, package them into a nice bundle of data called a URL and send it off to the backend.
When we launch an activity, we do a very similar thing. First, we get the name of which activity we want to open. Then we package together any extra information we might need to show this page into a nice bundle. We package these two pieces of information together into an object called an Intent and then we send it off to the Android OS using the very aptly named `startActivity` command.

Here's what an Intent looks like in code:
Intent(context, SearchActivity::class.java)
  .apply { putExtra("q", "duolingo") }
Ignoring the context for now, there are clear parallels between the Intent and the URL! Which page to open? SearchActivity. Bundle of misc. data? putExtra("q", "duolingo").

If you want to open a page in a browser from a URL you do:
window.open(
  "https://www.google.com/search?q=duolingo"
)
If you want to open an Activity in Android from an Intent you do:
startActivity(
  Intent(context, SearchActivity::class.java)
    .apply { putExtra("q", "duolingo") }
)

Comments

Popular posts from this blog

First-Principles Derivation of A Bank

A Play-by-play of the Mirai botnet source code - Part 1 (scanner.c)

You can control individual packets using WinDivert!