Monday, December 20, 2010

Android #8: Working with Android Intents - Part 3

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

Explicit Intents example: This example demonstrates the inter-activity communication and will show on invoking user defined custom activities from one 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:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/msg" />
<Button android:id="@+id/btnId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/invokeBtn" />
</LinearLayout>
layout/otherinvoked.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/otherMsgId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/otherMsg" />
</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;

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
Button invokeBtn=(Button)findViewById(R.id.btnId);
invokeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent explicitIntent=new Intent(BaseInvokingActivity.this, OtherInvokedActivity.class);
explicitIntent.putExtra("FromBaseInvokingActivity", "Value from FromBaseInvokingActivity");
startActivity(explicitIntent);
}
});
}
}
OtherInvokedActivity.java
package in.satworks.android.samples.activity;

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

public class OtherInvokedActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.otherinvoked);
Intent intent=getIntent();
Bundle extras=intent.getExtras();
String val=(String)extras.get("FromBaseInvokingActivity");
TextView txtView=(TextView)findViewById(R.id.otherMsgId);
txtView.setText("OtherInvokedActivity: "+val);
}
}
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="invokeBtn">Invoke Other Activity</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