From PHP to Android, the simple stuff – Intents and the POST-GET analogy

Featured Image

As mentioned in one of my previous articles, usually when you are trying out a different platform or language you try to find the analogy of what you done in your “yesterdays” programming language in regards to the new one.

One such cool analogy, as I see it, is the analogy of web’s POST & GET methods to the Androids “Intent” mechanism. Strange as it might seem, but I find those two pretty similar. Let me elaborate.

In web you have these forms with various input fields trough which you gather user data an submit them (via POST or GET) to some script. Usually this results in you ending up on the different page that was rendered based on the logic that handled the data you inputed on the previous page.

Android UI is completely different beast. One that enables you great deal of flexibility in building visual stuff presented to user. In a extremely simplified manner I would say two of the most common terms related to UI are Activity and Intents. One can (extremely simplified definition) say that Activity is a analogy to “Web page” concept while Intent is analogy to “POST or GET” actions.

One of the most essential things I asked my self on my first “hello world” android app was how to I “make and link” two different “pages” or more precisely two different Activities.

Below is a code example from initial Activity screen.

package net.ajzele.examples.webpageanalogy;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
 
public class WebPageAnalogyActivity extends Activity {
/**
* Freely taken id value
*/
    private static final int MY_COOL_ID_FOR_SOME_ACTIVITY = 2413;
 
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
 
Intent i = new Intent(WebPageAnalogyActivity.this, AnotherWebPageActivity.class);
i.putExtra("custom_key1", "My custom value for some field named custom_key1");
i.putExtra("key2", "My value for my field key2");
 
//Will start/run AnotherWebPageActivity Activity / "page" 
startActivityForResult(i, MY_COOL_ID_FOR_SOME_ACTIVITY);
    }
}

And here is the code example of the second Activity screen that gets called from the first Activity.

package net.ajzele.examples.webpageanalogy;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
 
public class AnotherWebPageActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page2);
 
        /**
         * Grab the "POST/GET" data or in Android terms grab the intent data
         */
        Intent i = getIntent(); 
        String field1Value = i.getStringExtra("custom_key1");
        String field2Value = i.getStringExtra("key2");
 
        Log.d("MY_DEBUG_INTENT_DATA_FIELD1_VALUE", field1Value);
        Log.d("MY_DEBUG_INTENT_DATA_FIELD2_VALUE", field2Value);
    }
}

And here (WebPageAnalogy) is the entire example app for you to play.

Cheers.


2 comments

  1. You know, it’s great!) I started reading your blog because I had to write some magento extensions, and yesterday I started exploring Android just for my own interest. And now I see android samples here.) Thank you)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.