0

I want the values of edit text restored when user comes back to my first activity? Please help me out.

Thanks in advance

this is my first activity code for getting user values in edit text

public class IntentActivity extends Activity {
EditText ed1, ed2;
float ed1_val, ed2_val;

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

    ed1 = (EditText) findViewById(R.id.editText1);
    ed2 = (EditText) findViewById(R.id.editText2);
    Button next = (Button) findViewById(R.id.button1);

    next.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(),
                    Second_activity.class);
            startActivity(intent);
        }
    });
}

/** Called when the activity is first created. */
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);

    ed1_val = Float.parseFloat(ed1.getText().toString());
    ed2_val = Float.parseFloat(ed2.getText().toString());

    Log.v("TAG", "inside saved instance");
    savedInstanceState.putFloat("ed1", +ed1_val);
    savedInstanceState.putFloat("ed2", +ed2_val);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    Log.v("TAG", "inside on restore");
    float ed_val = savedInstanceState.getFloat("ed1");
    float ed2_val = savedInstanceState.getFloat("ed2");

    ed1.setText("" + ed_val);
    ed2.setText("" + ed2_val);
    }
}

this is my second activity code

public class Second_activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_xml);

    Button back = (Button) findViewById(R.id.button1);
    back.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(),
                    IntentActivity.class);
            startActivity(intent);
            }
        });
    }
}
Shri
  • 149
  • 2
  • 4
  • 10
  • Do you want to send test from Second_activity to IntentActivity, or do you want to ensure that text entered into a text box in IntentActivity is restored when the Activity is resumed? – pmckeown Mar 27 '12 at 09:42
  • yes i want the text entered to be restored when i come back to the activity. – Shri Mar 27 '12 at 09:45

6 Answers6

3

It is not a good idea to start the first activity again on back pressed. Call finish() in the second activity. This will lead to the resume of the first activity which is what you need.

back.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        finish();            }
    });
}
user936414
  • 7,574
  • 3
  • 30
  • 29
  • but what if i have three activities and come back from third to first activity? – Shri Mar 27 '12 at 09:49
  • the onResume will not be called. Your activity will be started again. If you keep on pressing next and back buttons this may lead to lot of activity creations which are unnecessary. – user936414 Mar 27 '12 at 09:56
  • onRestoreInstanceState method will be called only when the activity's onStop() is called. Check the activity's lifecycle for more details. – user936414 Mar 27 '12 at 10:14
1

just finish the second activity don't start it via intent. When you finish the second activity first activity will be automatically resumed,

Ashwin N Bhanushali
  • 3,872
  • 5
  • 39
  • 59
1

you can go for overriding protected void onSaveInstanceState(Bundle outState) and protected void onRestoreInstanceState(Bundle savedInstanceState)

Here is an example

Remember it will work when you will not finish your previous activity.Do this in your first activity.

Android Killer
  • 18,174
  • 13
  • 67
  • 90
1

You don't need neither onSaveInstanceState nor onRestoreInstanceState. Just call finish in the onClick listener for the button in the second Activity:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class IntentActivity extends Activity {
    EditText ed1, ed2;
    float ed1_val, ed2_val;

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

        ed1 = (EditText) findViewById(R.id.editText1);
        ed2 = (EditText) findViewById(R.id.editText2);
        Button next = (Button) findViewById(R.id.button1);

        next.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),
                        Second_activity.class);
                startActivity(intent);
            }
        });
    }

}

This is the second one:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Second_activity extends Activity {
    // TODO Auto-generated method stub
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_xml);

        Button back = (Button) findViewById(R.id.button1);
        back.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
    }
}

That way you are resuming the previous Activity instead of starting new one. If you need to pass data between them you could use startActivityForResult / onActivityResult and setResult methods:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class IntentActivity extends Activity {
    private static final int GET_VALUES_REQUEST_ID = 1;
    public static final String FIRST_VALUE_ID = "first_value_id";
    public static final String SECOND_VALUE_ID = "second_value_id";
    private static final float DEFAULT_VALUE = 0;

    EditText ed1, ed2;
    float ed1_val, ed2_val;

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

        ed1 = (EditText) findViewById(R.id.editText1);
        ed2 = (EditText) findViewById(R.id.editText2);
        Button next = (Button) findViewById(R.id.button1);

        next.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),
                        Second_activity.class);
                startActivityForResult(intent, GET_VALUES_REQUEST_ID);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case GET_VALUES_REQUEST_ID: {
            if (Activity.RESULT_OK == resultCode) {
                ed1_val = data.getFloatExtra(FIRST_VALUE_ID, DEFAULT_VALUE);
                ed2_val = data.getFloatExtra(SECOND_VALUE_ID, DEFAULT_VALUE);
                setValues();
            }
            break;
        }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    protected void setValues() {
        ed1.setText(Float.toString(ed1_val));
        ed2.setText(Float.toString(ed2_val));
    }

}

The second activity could be something like that:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Second_activity extends Activity {
    // TODO Auto-generated method stub
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_xml);

        Button back = (Button) findViewById(R.id.button1);
        back.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent data = new Intent();
                data.putExtra(IntentActivity.FIRST_VALUE_ID, 324f);
                data.putExtra(IntentActivity.SECOND_VALUE_ID, 32234f);
                setResult(Activity.RESULT_OK, data);
                finish();
            }
        });
    }
}

This is a very basic example so I just hardcoded some return values - please implement something more meaningful. Beside that you could avoid using underscores as word separator in class names - camel case is much more accepted as name convention.

Samuil Yanovski
  • 2,837
  • 17
  • 19
  • The hardcoded value which is sent is getting printed, but i want to restore the data which the user has given as input in the edit text in my first activity. – Shri Mar 28 '12 at 07:21
  • Can you try the first examples I've given? :) If I've got you right they should do exactly what you need. – Samuil Yanovski Mar 28 '12 at 08:40
0

Its easy if you do not need to send data back to the first activity. How do send back data without using an intent.

0

You are starting a new activity by pressing back button on Second Activity. The new activity instance is not the previous one that has started the Second Activity hence onRestoreInstanceState callback is not getting called.

Relsell
  • 771
  • 6
  • 18
  • i guess thats the problem, but what should i do in that case? – Shri Mar 27 '12 at 09:52
  • just finish your Second Activity by calling finish() on pressing back button – Relsell Mar 27 '12 at 09:54
  • it works fine but what if i am sending bundle data in the intents? – Shri Mar 27 '12 at 10:00
  • 1
    In that case ..rather than using startActivity to start Second ACtivity from first user startACtivity for Result. Then while finishing second Activity set your bundled data. now in First Activity onActivityResult callback will be called before onRestoreInstanceState. – Relsell Mar 27 '12 at 10:11
  • i am not getting you, what do do in onActivityResult in the first activity? any example will be helpful? – Shri Mar 27 '12 at 10:15