Transfer data from one screen to another on android eclipse
Question
Hi am newbie to java and android.
Assume function demo()
from the screenone is going to display some values on Textview
which is on the same screen(screenone).
But i need to display that resultant value to the next screen ie.(screen two)
public void demo(){
{
.....
.....
}
So i have included these line to
screenoneActivity.java
Intent nextScreen = new Intent(getApplicationContext(), SecondtwoActivity.class);
nextScreen.putExtra("","");
startActivity(nextScreen);
demo();
ScreentwoActivity.java
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
TextView txtName = (TextView) findViewById(R.id.textView1);
Intent i = getIntent();
txtName.setText(name);
I did these things so far.I don't know how to do transfer data from demo()
function to the next screen.
Can anyone give me clues or ideas to achieve this.
Thanks a lot!..
Solution
You need to send some value into the putExtra methods parameters to be able to get something out of it.
In your first activity(A):
Intent i = new Intent(A.this, B.class);
i.putExtra("someName", variableThatYouNeedToPass);
startActivity(i);
In your second activity(B):
Bundle extras = getIntent().getExtras();
int fetchedVariable = extras.getInt("someName");
OTHER TIPS
Write the below code in demo() function:
Intent nextScreen = new Intent(getApplicationContext(), SecondtwoActivity.class);
nextScreen.putExtra("","");
startActivity(nextScreen);
In nextScreen.putExtra("","");
provide some key and value like:
nextScreen.putExtra("name","ABC");
Now in SecondActivity, write:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
TextView txtName = (TextView) findViewById(R.id.textView1);
Intent i = getIntent();
Bundle bundle = i.getExtras();
txtName.setText(bundle.getString("name"));
In ScreenoneActivity
Intent act2=new Intent(this,Activity2.class);
act2.putExtra("A",a);
startActivity(act2);
In ScreentwoActivity class
Intent i = getIntent();
Bundle extras = getIntent().getExtras();
int a = extras.getInt("A");
txtName.setText(a);
in onCreate :
Bundle extras = getIntent().getExtras();
String value;
if (extras != null)
{
value= extras.getString("key");
}
https://stackoverflow.com/questions/10752501/how-can-we-go-to-next-page-in-android/10752516#10752516
google it is very basic .....
android using intent....
in activity 1-
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");
startActivity(i);
In activity 2 - in onCreate finction
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
// Get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
// Do something with the data
}