-1

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!..

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
Manick
  • 817
  • 2
  • 15
  • 24

4 Answers4

2

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");
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
1

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"));
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
1

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);
swathi
  • 673
  • 1
  • 5
  • 7
0

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....

Vogella Ariticle

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
}
Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36