Sunday, August 7, 2011

[android-developers] How Can I keep data between my activities?

I have an activity what looks like this:

public class s extends Activity{

private static final int REQUEST_CODE = 1;

ImageView secret;
public int counter;
public int counter_foe;
public TextView txt;
public TextView counter_txt;
ImageView pic;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);

this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.scissors);

counter = 0;
counter_foe = 0;

pic = (ImageView) findViewById(R.id.pic);
final Button next = (Button) findViewById(R.id.next);
txt = (TextView) findViewById(R.id.txt);
secret = (ImageView) findViewById(R.id.secret);
counter_txt = (TextView) findViewById(R.id.counter_me);
final TextView counter_txt_foe = (TextView)
findViewById(R.id.counter_me_foe);

final Runnable r1 = new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
int imageArr[] = new int[3];

imageArr[0] = R.drawable.r1_mirror;
imageArr[1] = R.drawable.s1_mirror;
imageArr[2] = R.drawable.p2_mirror;

Random rand = new Random();
int rndInt = rand.nextInt(3);

secret.setImageResource(imageArr[rndInt]);

if (rndInt == 0) {
counter_foe += 1;
Log.d("improve", "counter_foe improved");
counter_txt_foe.setText(String.valueOf(counter_foe));
txt.setText("+1 for Me");
}

if (rndInt == 1) {
txt.setText("Draw");
}

if (rndInt == 2) {
counter += 1;
Log.d("improve", "counter improved");
counter_txt.setText(String.valueOf(counter));
txt.setText("+1 for You");
}

next.setVisibility(1);
}
};

final Runnable r2 = new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
next.setVisibility(-1);
}
};
next.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent("com.z.CHANCES");
startActivityForResult(i, REQUEST_CODE);
}

protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
if (data.hasExtra("r")) {
pic.setBackgroundResource(R.drawable.r1);
}
else if (data.hasExtra("p")) {
pic.setBackgroundResource(R.drawable.p2);
}
else if (data.hasExtra("s")) {
pic.setBackgroundResource(R.drawable.s1);
}
}
}

});

secret.postDelayed(r1, 5000);
}

}


And I have another one what looks like this:

public class possibilities extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.possibilities);

Button rock = (Button) findViewById(R.id.r);
Button paper = (Button) findViewById(R.id.p);
Button scissors = (Button) findViewById(R.id.s);

r.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
rfinish();
}

private void rfinish() {
// TODO Auto-generated method stub
Intent data = new Intent();
data.putExtra("r", "r");
setResult(RESULT_OK, data);
finish();
}
});

p.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent data = new Intent();
data.putExtra("p", "p");
setResult(RESULT_OK, data);
finish();
}
});

s.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent data = new Intent();
data.putExtra("s", "s");
setResult(RESULT_OK, data);
finish();
}
});
}

}


My problem is the following:
I want to keep my counter's and my counter_foe's content through the
activity changes. I don't want them to be the default (0) texts. For
example:
If I clicked on the next button of mine activity, then I called
startActivityForResult, and the another activity started. When I click
in the second activity on one of the possibilities, the first activity
created again. But with the main content (what is 0), and not with the
1, or 2, or 3 etc. How can I keep these contents and return them
everytime when the s activity resumed?

Thanks for any helps.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment