Creating An Android Application With Multiple Activities

Update to the Android Manifest File

Add the second activity definition within the application tag

<activity
android:name=”.SecondActivity”
android:label=”@string/title_activity_second” />

Update to the  strings.xml file

<string name=”title_activity_second”>SecondActivity</string>

Create SecondActivity.java

package com.example.list;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class SecondActivity extends Activity {

private String passedVarId = null;
private TextView textView = null;

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

passedVarId = getIntent().getStringExtra(“ID”);
textView = (TextView)findViewById(R.id.passedDataTextView);
textView.setText(“You Clicked on:”+passedVarId);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

Create activity_second.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent” >

<TextView xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:id=”@+id/passedDataTextView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textSize=”16sp” />

</RelativeLayout>

Call the second activity from the Main Activity [Refer to my ListView example]

In the OnItemClickListener method, instead of displaying the data in a popup – invoke the second activity

private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
i.putExtra(“ID”, String.valueOf(id));
startActivity(i);
}
};

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: