Thursday, December 23, 2010

Android #9: Working with Android Intents - Part 4

Refer the previous post to get background on Intents - http://satworks.blogspot.com/2010/12/android-6-working-with-android-intents.html

Explicit Intents with return example: This example demonstrates the inter-activity communication and will show on invoking user defined custom activities from one activity but instead of passing the control to the invoked activity, will stay on the current invoking activity with values updated from invoked activity



layout/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">
<TextView android:id="@+id/btnMsgId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/msg" />
<Button android:id="@+id/btnRtnId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/invokeRtnBtn" />
</LinearLayout>
BaseInvokingActivity.java
package in.satworks.android.samples.activity;

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;
import android.widget.Toast;

public class BaseInvokingActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Invoke other activity and Return
Button invokeRtnBtn = (Button) findViewById(R.id.btnRtnId);
invokeRtnBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent explicitIntent = new Intent(BaseInvokingActivity.this,
OtherInvokedActivity.class);
explicitIntent.putExtra("FromBaseInvokingActivity",
"Initial Value from FromBaseInvokingActivity");
startActivityForResult(explicitIntent, 1);
}
});
}
/** Called when an activity called by using startActivityForResult finishes. */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Bundle extras = data.getExtras();
String returnVal = (String) extras.get("OtherInvokedActivity");
Toast.makeText(BaseInvokingActivity.this, returnVal, Toast.LENGTH_SHORT)
.show();
}
}
OtherInvokedActivity.java
package in.satworks.android.samples.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class OtherInvokedActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent=getIntent();
Bundle extras=intent.getExtras();
String val=(String)extras.get("FromBaseInvokingActivity");
Intent modifiedIntent=new Intent();
modifiedIntent.putExtra("OtherInvokedActivity", val+":\nNow modified with this text included from OtherInvokedActivity");
setResult(1, modifiedIntent);
finish();
}
}
values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ExplicitIntentsApp</string>
<string name="msg">BaseInvokingActivity!</string>
<string name="otherMsg">OtherInvokedActivity!</string>
<string name="invokeRtnBtn">Invoke Other Activity and Return</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.satworks.android.samples.activity" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BaseInvokingActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="@string/app_name" android:name=".OtherInvokedActivity"></activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>

No comments:

Post a Comment