Sunday, October 6, 2013

Android-Returning Result from intent

Returning Results from an Intent

The startActivity() method invokes another activity but does not return a result to the current activity. For example, you may have an activity that prompts the user for username and password. The information entered by the user in that activity needs to be passed back to the calling activity for further processing. If you need to pass data back from an activity, you should instead use the startActivityForResult() method. The following Try It Out demonstrates this.

Returning Results from an Intent

1. Using the same project created in the previous section, add the following statements in bold to the main.xml file:

android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Please enter your name” />
android:id=”@+id/txt_username”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />

Returning Results from an Intent

2. Add the following statements in bold to Activity2.java:
package net.learn2develop.Activities;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Activity2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
//---get the OK button---
Button btn = (Button) findViewById(R.id.btn_OK);
//---event handler for the OK button---
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
Intent data = new Intent();
//---get the EditText view---
EditText txt_username =(EditText) findViewById(R.id.txt_username);
//---set the data to pass back---
data.setData(Uri.parse(txt_username.getText().toString()));
setResult(RESULT_OK, data);
//---closes the activity---
finish();
}
});
}
}

Returning Results from an Intent

3. Add the following statements in bold to the MainActivity.java file:
package net.learn2develop.Activities;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.KeyEvent;
import android.widget.Toast;
import android.content.Intent;
public class MainActivity extends Activity {
String tag = “Events”;
int request_Code = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//---hides the title bar---
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Log.d(tag, “In the onCreate() event”);
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
//startActivity(new Intent(“net.learn2develop.ACTIVITY2”));
//startActivity(new Intent(this, Activity2.class));
startActivityForResult(new Intent(“net.learn2develop.ACTIVITY2”),request_Code);
}
return false;
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
Toast.makeText(this,data.getData().toString(),
Toast.LENGTH_SHORT).show();
}
}
}
public void onStart() { //... }
public void onRestart() { //... }
public void onResume() { //... }
public void onPause() { //... }
public void onStop() { //... }
public void onDestroy() { //... }
}

Returning Results from an Intent

4. Press F11 to debug the application on the Android Emulator. When the first activity is loaded, click the center button on the directional pad. Activity2 will now be loaded. Enter your name (see Figure 2-16) and click the OK button. You will see that the first activity now displays the name you have entered using the Toast class.


Returning Results from an Intent

How It Works
To call an activity and wait for a result to be returned from it, you need to use the
startActivityForResult() method, like this:
startActivityForResult(new Intent(“net.learn2develop.ACTIVITY2”),request_Code);
In addition to passing in an Intent object, you need to pass in request code as well. The request code is simply an integer value that identifies an activity you are calling. This is needed because when an activity returns a value, you must have a way to identify it. For example, you may be calling multiple activities at the same time and some activities may not return immediately (for example, waiting for a reply from a server). When an activity returns, you need this request code to determine which activity is actually returned.
NOTE If the request code is set to -1, then calling it using the
startActivityForResult() method is equivalent to calling it using
the startActivity() method. That is, no result will be returned.
In order for an activity to return a value to the calling activity, you use an Intent object to send data back via the setData() method:
Intent data = new Intent();
//---get the EditText view---
EditText txt_username = (EditText) findViewById(R.id.txt_username);
//---set the data to pass back---
data.setData(Uri.parse(txt_username.getText().toString()));
setResult(RESULT_OK, data);
//---closes the activity---
finish();
The setResult() method sets a result code (either RESULT_OK or RESULT_CANCELLED) and the data (an Intent object) to be returned back to the calling activity. The finish() method closes the activity and returns control back to the calling activity.
In the calling activity, you need to implement the onActivityResult() method, which is called whenever an activity returns:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
Toast.makeText(this,data.getData().toString(),Toast.LENGTH_SHORT).show();
}
}
}
Here, you check for the appropriate request code and display the result that is returned. The returned result is passed in via the data argument; and you obtain its details through the getData() method.


Upload UIImage as base64 String

Upload UIImage as Base64 String (Upload UIImage as string) //Select Pic From Camera or Gallery       @objc func btnPro...