Monday 3 November 2014

Android Tutorial 37 : Starting Activity for Result in Android

In  Android an Activity can be started for some results, means an Activity can return some result to the Parent/Previous Activity.

An Activity can return String, Int, float, boolean  etc as Result.

In this post I have two Activities MainActivity   and SecondActivity.
Second Activty returns a String to the First  MainActivity as Result.
From MainActivity we start the Second Activity to get Result with follwing method

startActivityForResult(Intent  intent, int requestCode);


to get the Result back  we need to override the method..

 onActivityResult(int requestCode, int resultCode, Intent data)


the data object (last parameter of above method) contains the returned Result, we need to fetch the result from data(object of Intent).

See the Execution Flow

First Screen:   Click on get Message    (MainActivity)






Second Screen:   Enter the Message " I am much  Busy"   and click on Submit Message  (Second Activity)





3rd Screen :  See the returned message      MainActivity   , you can see the result is being returned to Previous Activity i.e. MainActivity (Which have started the activity for result)






Code:


activtymain.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textColor="#FF0000"
        android:textSize="20dp"
        android:text="Message" />

    <Button
        android:id="@+id/button1"
        android:layout_marginTop="20dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Get Message"
        android:onClick="getMessage" />

</LinearLayout>

layout2.xml   



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical" >

   
    
    <EditText
        android:id="@+id/editTextMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textColor="#FF0000"
        android:textSize="20sp"
        android:hint="Enter The Message" />

    <Button
       
        android:layout_marginTop="20dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Submit Message"
        android:onClick="submitMessage" />

</LinearLayout>


MainActivity.java


public class MainActivity extends Activity 
{
    TextView  textViewMessage;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // get The refference of The textView 
        textViewMessage=(TextView)findViewById(R.id.textViewMessage);
    }

    
    // Method to handle the Click Event on GetMessage Button
    public void getMessage(View V)
    {
            // Create The  Intent and Start The Activity to get The message
            Intent intentGetMessage=new Intent(this,SecondActivity.class);
            startActivityForResult(intentGetMessage, 2);// Activity is started with requestCode 2
    }
    
    
    // Call Back method  to get the Message form other Activity    override the method
    @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data)
       {
                 // TODO Auto-generated method stub
                 super.onActivityResult(requestCode, resultCode, data);
                 
    
                  // check if the request code is same as what is passed  here it is 2
                   if(requestCode==2)
                         {
                            // fetch the message String
                             String message=data.getStringExtra("MESSAGE"); 
                             // Set the message string in textView
                             textViewMessage.setText(message);
               
                         }
               
     }

}

SecondActivity.java



public class SecondActivity extends Activity 
{
    EditText  editTextMessage;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout2);
        // Get the Refference of Edit Text
        editTextMessage=(EditText)findViewById(R.id.editTextMessage);
    }

    
    
    public void submitMessage(View V)
    {
        
        // get the Entered  message
        String message=editTextMessage.getText().toString();
        Intent intentMessage=new Intent();
        
        
        // put the message to return as result in Intent
        intentMessage.putExtra("MESSAGE",message);
        // Set The Result in Intent
        setResult(2,intentMessage);
        // finish The activity 
        finish();
        
    }
    
 }

No comments:

Post a Comment