This is very basic knowledge to pass data from one activity to another activity, first we will create an android project then its activity and we will create an layout xml and associate activity lastly we will call the second activity from first one with the data.
If you are using eclipse
File --> New --> New Android Application
Then come to the layout folder and create an android layout xml file.
Then come to the main package folder in src and create an class file named as Second.
Your new layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
Your new activity class
package com.example.datapassexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Second extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView textView = (TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
if(intent.getExtras() != null)
{
String text = intent.getStringExtra("name");
textView.setText(text);
}
}
}
Your first activity class
package com.example.datapassexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText1 = (EditText) findViewById(R.id.editText1);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClassName(getApplicationContext(), Second.class.getName());
intent.putExtra("name", editText1.getText().toString());
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
!!!!!!
The value that you pass in activity depends on the passed activity so there will be no value in another activity associated with this intent extra even if you use getIntent() function instead of new Intent() function.
So how can i get the same value in another activities?
There are lots of ways to get data in another activities such as you can write to your sqlite database or you can pass using intents again even you can send to any server you defined then you get the value from that server but the fast way is that i also use the singleton method you can store the value among the usage of program if there are some states that the program will be cancelled you should use db. Singleton is like below
open a new class named as Single
package com.example.datapassexample;
public class Single {
private static Single single;
private String name;
protected Single(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static Single getInstance(){
if(single == null){
single = new Single();
}
return single;
}
}
open your MainActivity
add this line into the button click function before startActivity function
Single single = Single.getInstance();
single.setName(editText1.getText().toString());
open your Second Activiy then insert code below
Single single = Single.getInstance();
String text = single.getName();
textView.setText(text);
Now you can use the value that you store in Single class in each of the activities for active states of the program.

