Rookie
Rookie

Reputation: 37

How to save an int for another activity?

I have one activity where the user writes in an EditText a number, then I am supposed to convert it to a string and then to an int, but I want to save that value because I am going to use it for another activity, it will be the max number of times that a botton can be pressed but I don't know how to save it from the EditText in a activity to an int in another activity. Here the EditText of the first activity:

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/texthome"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:hint="Ejemplo:15"
        android:layout_weight="1"
        android:textSize="20sp" />

Here the textview into int:

EditText conttext = (EditText) findViewById ( R.id.texthome );
String maxicont = conttext.getText().toString();
int maxcont = Integer.parseInt(maxicont);

UPDATE: i get errors in:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Button buttsave = (Button) findViewById(R.id.buttsave);
    buttsave.setOnClickListener(new View.OnClickListener"here" {

        public void onClick "here" {
            Intent intent = new Intent(getApplicationContext(), GuessOne.class);
            intent.putExtra("maxNumberPressed", conttext.getText());
            startActivity(intent);
        }
    });

I get errors where i placed the "here" obviously it is not part of the code. It is only to see where the errors are.

Upvotes: 0

Views: 159

Answers (3)

Kenneth Streit
Kenneth Streit

Reputation: 1272

When starting the other activity, make a bundle.

In the current activity, try:

Intent intent = new Intent(getApplicationContext(), <next activity>.class);
intent.putExtra("maxNumberPressed", editText.getText());
startActivity(intent);

Then in the next activity where you are using it, put this in onCreate method:

Bundle bundle = this.getIntent().getExtras();
String maxPressed = bundle.getString("maxNumberPressed");

Create a button with text as 'save' or something. then do:

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener{
    public void onClick() {
        Intent intent = new Intent(getApplicationContext(), <next activity>.class);
        intent.putExtra("maxNumberPressed", editText.getText());
        startActivity(intent);
    }
});

Upvotes: 0

SMBiggs
SMBiggs

Reputation: 11688

How to pass data from an Activity to another can vary a lot, depending on how the Activities are related.

  1. If you are passing data from a parent to a child Activity, you pass the information through an Intent when starting the new child Activity. Here's how you send the information:

    itt = new Intent(this, ChildActivityName.class); // Create new Intent itt.putExtra("MAX", maxcont); // Fill Intent with data startActivity(itt, SOME_NUMBER_IDENTIFYING_THE_PARENT); // Start Activity

And here's how the receiving child Activity retrieves it:

void onCreate(Bundle savedInstanceState) {
    ...
    Intent itt = getIntent();  // Grab the Intent created with this Activity
    int maxcont = itt.getIntExtra("MAX", -1); // Retrieve data, default is -1
}
  1. If you are passing data the other way, from a child to a parent Activity, the simplest way to pass through using startActivityForResult. The parent starts the child activity using this command:

    startActivityForResult(itt, SOME_NUMBER_IDENTIFYING_THE_PARENT);

The Child then passes the information back to the parent just before it exits:

Intent itt = new Intent();            // Create new Intent
itt.putExtra("MAX_RETURN", maxcont);  // Add data to the Intent
setResult(RESULT_OK, itt);            // Signal to parent valid result
finish();                             // End this Activity

Once the child calls finish(), the parent's onActivityResult() method will be invoked:

protected void onActivityResult(int requestCode, int resultCode, Intent itt) {
    maxcont = data.getIntExtra("MAX_RETURN", -1);  // defaults to -1
    ...

}

The Strings used in putExtra and getIntExtra are identifying keys for associated data, which are used all over the place in Android programming and beyond the scope of this question. Just make sure that the sender and the receiver of the data through the Intents use the EXACT same string!

  1. If you need to pass information between two sibling Activities that are both active, the easiest way is through static public variables in each Activity. That's pretty basic; you should have no problem with these.

  2. If you need to pass information between two sibling Activities that may or may not be Active, then you need to use a Broadcast/Receiver paradigm. But I think that method is overkill for what you are talking about.

Upvotes: 0

mcsilvio
mcsilvio

Reputation: 1098

You need to use Intents for that. A similar question was answered here:

Passing Values To Another Activity

Upvotes: 1

Related Questions