Wednesday, June 8, 2011

How to use Intent in the applications

In today's discussion we are going to use Intent function in the Activity class.
First of all i would like to say some information about the Intent class and it's useful in real time applications.
Consider an example we try to display some screen shot in the opening page after that i want to move from one screen to another screen then in that case we should use the Intent class .

See the following example how the intents are useful to move from one screen to another screen.
Create one application with some name like IntentExample.In that application try to create java class as MainActivity.java and this activity should extend Activity class.

//MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(MainActivity.this,NextActivity.class);
               
                
            }
        });
    }
}

I will explain some information about the above java code.

Generally if we create any activity then our activity should extend Activity class and out activity class can override some functions in activity class those are like onCreate(..),onStart(),onResume(),onPause(),onStop(),onDestroy().These functions are important for any activity here onCreate() function is called whey the application is first time started and from next time when we interact with application then onStart() function is executed and onResume() function is when we put the appliaction in pause the we get the information back...etc.........I will explain about every function in the following tutorials now just try to know about the basic knowledge of these functions.
      In the above described java code we used only onCreate() function.I think u may get the doubt like what about the onStart() function here in our code it is not used.............yes it is there in Activity class so when the application calls the onStart() function first it check's our MainActivity.java class if it is not there in our java class it check's in the Activity.java class since this function is already described in Activity class this function is called in the Activity class............Ok

how to call the next screen or next activity from our java class is as follows.

Intent intent=new Intent(MainActivity.this,NextActivity.class);

In my application i created a button in the main.xml file and the object of that function is taken by findViewById() function.So findViewById(...) function is used to get the xml creted objects .
In the next step i created one function in such a way that when i clicked on the button the onClick function is called so automatically that function executes.
In the onClick() function i called the Intent class. 

Intent intent=new Intent(MainActivity.this,NextActivity.class);
So here i wrote the logic for calling the next screen or next activity.So when creating the Intent object itself we have to give the current class context object as MainActivity.this .Here MainActivity.this is nothing but context object of current class and the next attribute is NextActivity.class file name.So we have to create one more class with NextActivity.java and this activity also should extend Activity class and should override onCreate() fuction so when we are creating new activity then we should extend Acitivity class and should override onCreate() function these are basic try to remember.
NextActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class NextActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
       
    }

}


In the next step configure the main.xml file
//main.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"
    android:background="#ffffff"
    >

<Button android:text="Next"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        ></Button>
</LinearLayout>
Here main.xml is used by MainActivity.java.Similarly we have to create one more xml file in res/layout folder.Here i created with the name main1.xml .
//main1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  >
   <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="we are now in NextActivity.java screen"
    />
</LinearLayout>


so main.xml is configured in MainActivity.java and main1.xml file is configured in NextActivity.java.Here one thing should be clear when we all any application first it calls MainActivity.java file and from here it call the main.xml file by using setContentView(....) function.So xml files are used to display the static component's like buttons,images ....etc but if we want to make those static components as dynamic then we should use Activity class.

Remember after creating NextActivity.java it's entry is configured in AndroidManifest.xml file as given below
<activity android:name=".NextActivity" ></activity>
In today's discussion we got some knowledge about Intent class and in tomorrow's class we have to discuss about how to move from first screen to next screen and form next screen to first screen.


IF U HAVE ANY DOUBTS THEN PLS SEND REPLY TO THIS AS COMMENTS.......................






No comments:

Post a Comment