Friday, March 09, 2012


Android Activity and onActivityResult



The startActivity(Intent) method is used to start a new activity, which will be placed at the top of the activity stack. It takes a single argument, an Intent, which describes the activity to be executed.
      Sometimes you want to get a result back from an activity when it ends. 


    For example,we may start an activity that lets the user pick data from a list; when it ends, it returns the selected data to the first activity. To do this, you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. 

    The result will come back through your onActivityResult(int, int, Intent) method.


Example:  

 MainActivity.java
  Intent intent = new Intent();
intent.setClassName("com.mypackage.MainActivity", "com.mypackage.ListActivity");
startActivityForResult(intent,100);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("result code----------"+resultCode+" req code---"+requestCode);
  //Functions to be done
   }
ListActivity.java
In onItemClickListner just add the following code:
setResult(123);        
finish();      

No comments:

Post a Comment

Google Associate Android Developer Certificate - GCAAD

Google Associate Android Developer Certificate - GCAAD Now Google is allowing developers to complete on-line course and attend exam o...