Saturday, 21 July 2012

Android Intent Tutorial

Introduction of Android Intent


Situation : A company wants to recruit employee for any project. so it use it's advertisement service. Any person who is suitable can apply for this job and complete company project.

Conclusion : Here advertisement service works as intent. which is a interface between company and employee. means intent is a way to pass information in android plateform. As company can recruit employee as it's requirement and then publish advertisement. So intent is run time entity.

We can say intent as asynchronous message (information) passing in android system. This message is passed among android activity, services, broadcast receiver etc.

syntex :
 Intent intent = new Intent(context,NameOfJavaClassToCall.class);
 startActivity(intent);
From above we need to create standard intent object which contain current context and name of java class to be called. Then we need to use startActivity() method to call new activity. In this method we pass out intent object.

How to pass message(information)

For passing information between two android activity we use android putExtra() method which contains a key-value concept.

Syntex :
Intent intent = new Intent(context,NameOfJavaClassToCall.class);
intent.putExtra("key","value");
startActivity(intent);
How to receive intent into another class 

For receiving information into another class we use android getIntent().getExtras() method. This method return message in form of bundle. So we need to create bundle object to get intent. Then we usegetString() method with desired key to get data.
Syntex :

Bundle extras = getIntent().getExtras();
String value = extras.getString(key);


Example
  1. Make layout file(main.xml) which contains a button. If we click on button it change to screen2.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
     
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="screen 1 (main.xml)"/>
     
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Click me to another screen" />
     
    </LinearLayout>
    
  2. Make layout file(main2.xml) which contains a button. If we click on button it change to screen1.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
     
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="screen 1 (main.xml)"/>
     
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Click me to another screen" />
     
    </LinearLayout>
    
  3. Then apply onclick event in mainactivity.class to switch screen 1 to screen 2

    public class mainactivity extends Activity {
     
    	Button button;
     
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		addListenerOnButton();
    	}
     
    	public void addListenerOnButton() {
     
    		final Context context = this;
     
    		button = (Button) findViewById(R.id.button1);
     
    		button.setOnClickListener(new OnClickListener() {
     
    			@Override
    			public void onClick(View arg0) {
     
    			    Intent intent = new Intent(context, mainactivity2.class);
                                startActivity(intent);   
     
    			}
     
    		});
     
    	}
     
    }
    
    
  4. Then apply onclick event in mainactivity2.class to switch from screen 2 to screen 1

    public class mainactivity2 extends Activity {
     
    	Button button;
     
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main2);
    		addListenerOnButton();
    	}
     
    	public void addListenerOnButton() {
     
    		final Context context = this;
     
    		button = (Button) findViewById(R.id.button1);
     
    		button.setOnClickListener(new OnClickListener() {
     
    			@Override
    			public void onClick(View arg0) {
     
    			    Intent intent = new Intent(context, mainactivity.class);
                                startActivity(intent);   
     
    			}
     
    		});
     
    	}
     
    }
    
    
  5. Output : 






No comments:

Post a Comment