Page 1 of 1

Activities, Intents and Something on Events

#1 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Posted 23 March 2011 - 01:43 PM

Table of contend
  • Newbie?
  • Creating Layouts and Activities
  • Something on Events
  • Using an Intent
  • Intent's and Extras
  • Done


Newbie?
To absolve this Tutorial, a basic understanding of Android, Activities and Layouts is needed. I suggest, you read those Tutorials by gabehabe:


Creating Layouts and Activities
Now that you basically know how to do things on Android, we're going to create the two Activities that we're working with, their Layout's and the used Strings. I created a new Android-Project in Eclipse and called it "ActivitysAndIntents".

Main.java
This is the Main-Activity, which shows up when the App is started. This Code is mostly basic stuff to create the Activity:
package dic.tutorial.Intends;

import android.app.Activity;
import android.os.Bundle;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
    }
}


The Main-Layout:
The Layout for the Main-Activity, written in XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<TextView  
    	android:layout_width="fill_parent" 
    	android:layout_height="wrap_content" 
    	android:text="@string/hello"
    />
    <Button
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:text="@string/button"
    	android:id="@+id/start"
    />
</LinearLayout>



The "Hit"-Activity
This is the second Activity we're going to "call":
package dic.tutorial.Intends;

import android.app.Activity;
import android.os.Bundle;

public class Hit extends Activity{
	
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		this.setContentView(R.layout.hit);
	}

}



The Hit-Layout
The Layout for the "Hit"-Activity, written in XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  
  <TextView
  	android:layout_width="fill_parent"
  	android:layout_height="wrap_content"
  	android:text="@string/hit"
  	android:id="@+id/hit"
  />
</LinearLayout>



The Strings
Last but not least, the XML-File, containing the used Strings (important if you want to make your App multilingual):
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Simple Demo on Activitys and Intets!</string>
    <string name="app_name">ActivitysAndIntents</string>
    <string name="button">Fire the Intent!</string>
    <string name="hit">
    	Just just fired an Intent!\n
    	Use the "Back"-Button and do it again!
    </string>
</resources>



Something on Events
So, now that we have our basic components, lets code! The App should start in the Emulator now and you'll see the Main-Activity. There should be some Text and a Button which does... wait for it... nothing!

So now, we're about to change that. In the Tutorial "Simple UI with Events", gabehabe used a onclickListener, which is almost the same as a ActionListener known from Java. Well, in Android, there's another (maybe cooler) way to make a Button do something: You can specify the function that should be called when the Button is clicked, in the XML-File.

To do that, where going to add something to the Main-Layout:
<Button
  android: [...]
  android:onclick="fire"
/>


So, thats it? Yep, almost. Now we need to create the Method in the Main-Activity. A method, which should be called like this needs to be public and void. Also, a Parameter of the Type View will be passed to the Method:
public void fire(View view){
    Toast.makeText(this, "FIRE!", Toast.LENGTH_SHORT).show();
}


Now, if you start the App in the Emulator and click the Button, a Toast (yes, it's really Toast!) should show up. This is much easier and maybe even faster then always calling the onclick-Method and searching for the correct Button... And, the Code looks way more simple.

Using an Intent
Okay, now that we have our Activities, our Layouts and the Button up and running, it's time to use an Intent!

Android is a component-based System. Every Activity represents a component. Of course, you need something to communicate between those Components. That's where the Intent comes into play. Intent's are used to call/open a Component from the same App, or even from another App (for example the System-Call App). To open Activities from other Apps, your App needs special rights... But that's an other story...

Now, how can we use an Intent to open the second Component from our App, which is the Hit-Activity?
public void fire(View view){
  // Create the Intent
  Intent i = new Intent(this, Hit.class);
  // Start the other Activity, using the Intent
  this.startActivity(i);
}


The Constructor of the Intent-Class awaits two parameters: The application-context and the Class, which contains the Activity.

In this case, we can use this for the application-context, because we are in a Activity, and the Activity-class extends the Context-class. If this is not available, the method getApplicationContext() will do the job.

After we created the Intent, we used the startActivity(Intent i)-method to open the specified Activity. If you test this, you'll see something like this in your Emulator:

Attached Image

This Error is thrown, because Android doesn't know the Activity. We need to declare it as an Activity, in the AndroidMainfest of our Application:
<application [...]>        
  <activity android:name=".Hit" />
</application>


Now that the Activity is declared in the Manifest, the App should work and you should see the other Activity.

Intent's and Extras
But what, if we wanted to pass the opened Activity some values? Well, Activities can have Extras, which are almost like little Boxes where you can put some stuff in and then send it with the Intent. The opened Activity can unpack the box and use the Stuff that was inside. A little example of how this could be done:

Sending the Intent
I used just a String, you can also create a new EditText and get the Text from it...
public void fire(View view){
    // Create the Intent
    Intent i = new Intent(this, Hit.class);
    // Put the Extra
    i.putExtra("SOME_EXTRA", "Change ME!");
    // Send the Intent
    this.startActivity(i);
}


Receiving the Intent
Now, we get the Extra from the Intent. This is done in the onCreate()-method:
public void onCreate(Bundle bundle){
	super.onCreate(bundle);
	this.setContentView(R.layout.hit);
	// get the Extra from the Intent:
	Bundle extras = this.getIntent().getExtras();
	// Check if there is a Package and
	// if the String is in it:
	if (extras != null && extras.containsKey("SOME_EXTRA")){
		// Get the TextView to display it:
		TextView show = (TextView) this.findViewById(R.id.hit);
		// Get the String from the Package:
		String text = extras.getString("SOME_EXTRA");
		// Show it on the TextView
		show.setText(text);
	} else {
		// Make some Toast:
		Toast.makeText(getApplicationContext(), "Something went wrong...", Toast.LENGTH_SHORT);
	}
}


The Extra-Boxes are like HashMap's in Java (Link).

There are putExtra()-methods for many Java-Data-Types. However, passing an Object is a little more complicated (See here).

Done
... jay, not really. Intents can do more than just open another Activity. For example, you can call/open a System-Activity (like the Dailer or the Contact-Book) or an Activity from another App with an Intent. Also, there's another technique called "Sub-Activities", but thats stuff for like one or two more Tutorials, just focusing on those things. Let's see what time brings with it...

If you have any suggestions about "How to do it better" or "That's not correct", don't hesitate to answer this post. Also, I'd like to have some feedback on my writing-stile...

Greetings and Happy Coding: Luke

Is This A Good Question/Topic? 1
  • +

Replies To: Activities, Intents and Something on Events

#2 lexxe  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 44
  • Joined: 05-December 10

Posted 26 April 2011 - 08:30 AM

Very nice tutorial. I don't have a mouse to navigate things on my computer to try this out, but after lunch the receptionist is bringing me mouse batteries and I'll give it a try. Its very clear and gives a very nice route to getting activity switching a go.
Was This Post Helpful? 0
  • +
  • -

#3 dhussey111  Icon User is offline

  • New D.I.C Head

Reputation: -40
  • View blog
  • Posts: 33
  • Joined: 22-September 12

Posted 08 October 2012 - 09:33 AM

very nice step by step .. thanks for a lovely post..
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1