0

I have two android activity. Activity1 is a input form.In this activity have a manyTextEditor and ImageView. but one of many TextEditor need go to Activity2 for fill a data and return a data to Activity1.

But when the user returns to Activity1 a value in TextEditor of user has fill it before go to activity2 it be lost.

how to still a value of TextEditor when go to another Activity.

thank for any idea.

King_Dark
  • 299
  • 1
  • 4
  • 14
  • Create a `Constant class` and make `String variable` and store this value in this and load this value again when your `onResume()` activity and saved on `onPause()` – M D May 14 '14 at 11:01

4 Answers4

3

Here is official guide:

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

You should store text edit value inside Bundle in:

protected void onSaveInstanceState (Bundle outState)

http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29

and restore it from savedInstanceState bundle in:

protected void onRestoreInstanceState (Bundle savedInstanceState)

http://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState%28android.os.Bundle%29

[edit]

actually android should save your view content using above mechanism

marcinj
  • 48,511
  • 9
  • 79
  • 100
0

when you go to next activity, store that value in sharedpreferences and when again back to previous activity retrieve it from there and put that in edittext.

Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
0

Writing it down in code as suggested by Simple Plan

    public class MainActivity extends Activity {

    EditText ed;
    Button bt;
    private String str = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed = (EditText) findViewById(R.id.editText1);
        bt = (Button) findViewById(R.id.button1);



        bt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent (MainActivity.this, First.class);
                startActivity(i);
            }
        });

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        str = ed.getText().toString();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        ed.setText(str);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
Aniruddha
  • 4,477
  • 2
  • 21
  • 39
0

OK. I can solved my problem.

I use a startActivityForResult() function to get user data from subActivity and return result back to main activity this ways a value in TextEditor of user has fill it before go to activity2 It doesn't gone

Referrence:How to pass integer from one activity to another?

PS. Thank for marcin_j answer it as a new knowledge for me and thank for any comment and answer

Community
  • 1
  • 1
King_Dark
  • 299
  • 1
  • 4
  • 14