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"?>layout/otherinvoked.xml
<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>
<?xml version="1.0" encoding="utf-8"?>BaseInvokingActivity.java
<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>
package in.satworks.android.samples.activity;OtherInvokedActivity.java
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);
}
});
}
}
package in.satworks.android.samples.activity;values/strings.xml
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);
}
}
<?xml version="1.0" encoding="utf-8"?><resources>AndroidManifest.xml
<string name="app_name">ExplicitIntentsApp</string>
<string name="msg">BaseInvokingActivity!</string>
<string name="otherMsg">OtherInvokedActivity!</string>
<string name="invokeBtn">Invoke Other Activity</string>
</resources>
<?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