simplyeasylearning

Tuesday 4 November 2014

Use of Intents and How it works.

                                Intents are a mechanism in android to invoke new activities from one Activity.Through Intents we can pass info from one intent activity to another activity, the reverse can also be done within intents,where data can be received back from the child intent to parent intent.There are primarily two types of intents which we can use in android:-
1) Explicit Intents
2)Implicit Intents



1)Explicit Intents-
                                They are those intents which are created by user or may also be called as a user defined intent ,we can have multiple activities within an android application and all those activities can be called in the form of intents on the happening of certain events.
                                  The intent would automatically need a layout that is essential for every activity. 

Let's make a simple application on Explicit Intents-

step-1:
Crete a new project and name it 
Step 2:
Go to activity_main.xml  and then GO to design and take a Button widget

Step 3:

First Create a Child Activity or Second Activity  ,Right Click on Java Folder on Left side Menu in Android Studio    New->Activity->Blank Activity        and name it Child Activity
So a new Activity  Child_Activity.java is created.

Go to Main_Activity.java

And Write following code-


public void CallChildActivity(View v)
{
    Intent ChildActivity=new Intent(getApplicationContext(),ChildActivity.class);           
    ChildActivity.putExtra("UserID","LPU");
    ChildActivity.putExtra("Password","Ashish");       

    startActivity(ChildActivity);          
//for calling up the child activity

}



Go to activity_my.xml :-

Go to design and take a button  and Take two EditText widget  and give them id editText and editText2 respectively by which we can call our child activity and enter some data and send it to our Child Activity.


<Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My Introduction"
        android:id="@+id/button"
        android:layout_below="@+id/button"
        android:layout_alignParentStart="true"
        android:layout_marginTop="68dp"
        android:onClick="CallChildActivity"/>        //Add this line for creating a method related to this button


Go to Child_Activity.java:-


Write the code below this method:-

public class ChildActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_child);



//WRITE YOUR CODE BELOW THIS FOR FETCHING DATA INTO CHILD ACTIVITY

 I am writing this code in Child Activity For fetching data from Main Activity.

 EditText et1=(EditText) findViewById(R.id.editText);
        EditText et2=(EditText) findViewById(R.id.editText2);
        Intent in=getIntent();
        String vUsername=in.getExtras().getString("UserID");
        String vPass=in.getExtras().getString("Password");
        TextView tv=(TextView) findViewById(R.id.textView);
        tv.setText(vUsername);
        et1.setText(vUsername);
        et2.setText(vPass);
    }




2)Implicit Intents:-
                                 They are those which are not user created and these intents can be called by any application by making a single call, there are lot many implicit intents which can be called.
For example:-Browser Intent ,SMS intent ,Call Intent.

                              We can capture these intents so that out application can make use of inbuilt intents,whenever certain interaction is required .We can call these implicit intents in the following manner-:

Calling Up Browser:-

This intent will invoke  by default browser configure within your android application we can use the intent class to invoke the same .




 
                                

0 comments:

Post a Comment

Popular Posts

Total Visitors

TutorialsHub. Powered by Blogger.

Contributors

Tutorials

Search This Blog