Saturday, April 7, 2012

Start Activity from Activity


Some time we need to start an Activity from Activity..
how can we achieve this i am going to write step by step.

We will create very simple app to do this work
 -------------------------------------------
App Name: Activity2Activity
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: FirstActivity
-------------------------------------------


FirstActivity.java

package com.rdc;

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 FirstActivity extends Activity implements OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);

Button btnload = (Button) findViewById(R.id.btnfirst);
btnload.setOnClickListener(this);

}

@Override
public void onClick(View v) {
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);
}
}

SecondActivity.java
package com.rdc;

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 SecondActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);

Button btnload = (Button) findViewById(R.id.btnsecond);
btnload.setOnClickListener(this);

}

@Override
public void onClick(View v) {
Intent i = new Intent(SecondActivity.this, FirstActivity.class);
startActivity(i);
}
}

first.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:text="I am first activity.."
android:gravity="center"
android:layout_height="62dp" />
<LinearLayout
android:id="@+id/layV"
android:orientation="vertical"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Load Second Activity"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnfirst"></Button>
</LinearLayout>

</LinearLayout>



second.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:text="I am Second activity.."
android:gravity="center"
android:layout_height="62dp" />
<LinearLayout
android:id="@+id/layV"
android:orientation="vertical"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Load First Activity"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnsecond"></Button>
</LinearLayout>

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />

<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".FirstActivity"
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:name=".SecondActivity"
android:label="@string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
</manifest>

The output Screen will be like this..


You can download the complete source code zip file here : Activity2Activity

 cheers!!

 I'd love to hear your thoughts!

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home