0

Screen2.java file has the following code :

public class screen2 extends Activity {

    public int globalZip=0;
        //Some validations & update globalZip
        //Code control goes to Screen3,java

}

Screen3.java file has the following code :

public class Screen3 extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen3);
        screen2 objs2= new screen2();
        int myzip = objs2.globalZip;

        Toast.makeText(getBaseContext(), "Screen3 "+myzip, 5).show();

        System.out.println("WTHDude"+"Screen3 "+myzip);

    }

Now the problem i am having that if i have updated value of globalZip in Screen2.java file as 90034 it is not getting updated in the screen3. Can anyone help me with this error. Thank you.

2
  • This is because you are NOT updating value of globalZip in Screen2 instance which you are using in Screen3. Commented Nov 20, 2011 at 13:29
  • @typedef1: "screen2 objs2= new screen2();" - NEVER use this sort of code to create an instance of an Activity. As mentioned in the answers, create an Intent and add 'extra' data to it then pass it to startActivity(...) or startActivityForResulrt(...) Commented Nov 20, 2011 at 13:58

2 Answers 2

5

Well, you're creating a new instance of Screen2, so of course you will return the initial value of globalZip since it is not a static class member.

Saying that, you probably don't really want it to be a static member anyway.

But more importantly you're going about this completely wrong. If you want to pass data from one Activity to another Activity you just need to, for simple data like Strings/booleans/ints, add it to the Intent that starts Screen3.

Something like this:

// inside Screen2.java
Intent intent = new Intent(this, Screen3.class);
intent.putExtra("screen2.globalzip", globalZip);

And then to get the value in Screen3.java:

Bundle extras = getIntent().getExtras();
int globalZip = extras.getInt("screen2.globalzip");
Sign up to request clarification or add additional context in comments.

Comments

1

Use intents for passing data between activities. Here is and example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.