0

I abstract my model like this:first there is a class UserInfo which holds user information:

public class UserInfo extends Application{
private int userid;
public void setUserId(int id)
{
    userid=id;
}
public int  getUserId()
{
    return userid;
}

}

Then in MainActivity:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    userinfo=(UserInfo)getApplication();
    userinfo.setUserId(1354);
    ....
    Intent intent=new Intent(MainActivity.this,VoteActivity.class);                 
                startActivity(intent);
}
public
@Override void onResume()
{
    super.onResume();
    TextView text=(TextView)MainActivity.this.findViewById(R.id.usernameText);
    text.setText(userinfo.getUserId()+" ");
}

And in VoteActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vote);        
    userinfo=(UserInfo)getApplication(); 
    Button back=(Button)findViewById(R.id.backButton);
    back.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            userinfo.setUserId(-100);
            Intent intent=new        Intent(VoteActivity.this,MainActivity.class);
            startActivity(intent);

        }});

}

The result is: when MainActivity first run, userid in UserInfo is 1354 ; And when VoteActivity first run, userid in UserInfo is 1354 too.However when back to MainActivity from VoteAcitivy userid remains 1354 which should be -100.What is wrong with this use of Application context?

huaxz1986
  • 135
  • 1
  • 1
  • 6

3 Answers3

1

You're starting a new activity in your onClick method :

Intent intent=new        Intent(VoteActivity.this,MainActivity.class);
        startActivity(intent);

And you direct set your id in the MainActivity :

 userinfo.setUserId(1354);

That's why you get (1354). You should call finish in your on click (instead of starting the MainActivity).

The activity's stack look like this : MainActivity - VoteActivity - MainActivity and I think you want it to look like this : MainActivity after pressed on your Button

@Override
        public void onClick(View arg0) {
            userinfo.setUserId(-100);
            finish();

        }});
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • While this is certainly odd, that would not explain the behavior the OP is seeing. – CommonsWare May 14 '13 at 14:34
  • How can the userId be -100 if he sets it to 1354 directly after ? I can't see in how case you can get -100 by returning to the previous activity. (maybe I'm missing something) – Alexis C. May 14 '13 at 14:38
  • 1
    when call finish() then onResume is get called so textview data is not updated and it will show old data because we did not get latest Instance of UserInfo – Pawan Yadav May 15 '13 at 06:00
0

How you do you get back for your MainActivity? Pressing the device's back button or clicking on your button? If you press the device's back button your onClickListener won't be called, so that's why id remains the same.

Bruno Mateus
  • 1,727
  • 18
  • 25
0
@Override
public void onClick(View arg0) {
    userinfo.setUserId(-100);
    Intent intent=new        Intent(VoteActivity.this,MainActivity.class);
    startActivity(intent);

}});

when you click on your back button you restart MainActivity. The MainActivity onCreate will be excuted again. Is MainActivity flagged as singleInstance?

Blackbelt
  • 156,034
  • 29
  • 297
  • 305