Question

I need to fill form and by pressing "Back" button save it , let's say, to preferences. Or even show it to Log.

The problem is when I use onBackPressed() - Activity goes to onPause() and myEditText.getText().toString() returns result from XML and not the result that was actually in myEditText field. Same thing goes for onDestroy().

Of course, if I hang someButton.onClickListener() it saves my data well from myEditText because Activity doesn't go onPause() and doesn't take result from XML, but I need to save it exactly by pressing back button.

The code is basic:

    EditText etCompetitionName;
Competition competition;
etCompetitionName=(EditText) findViewById(R.id.etCompetitionName);
 Intent intent=getIntent();//I get number of object to work with
pos=intent.getIntExtra("pos", -1); // this is this number
  competition=getObject(pos);// well, this is loading this object
 olDetCompetitionName=competition.getName();//getting name from loaded object
          etCompetitionName.setText(olDetCompetitionName);//set this text to EditText field
...
then :

protected void onDestroy() {
    super.onDestroy();
//try to save

competition.setName(etCompetitionName.getText().toString());
        saveObject(competition);
}    

So, it saves old value, the one I set in the beggining. If I don't set it - it will be taken from layout xml. It is issue of onPause() I need to avoid it.

Was it helpful?

Solution

Your question is quite confusing but if you override onBackPressed() you can get the data.

@Override
public void onBackPressed()
{
     String text = myEditText.getText().toString();
     // save text
     super.onBackPressed();
}

I know you said you did this but show how you are doing it if this doesn't work. This will put whatever the user has typed in the text variable then you can save it in SharedPreferences or wherever you want.

You can also do the same thing in onPause() if you want it to save if, say, something else comes in the foreground.

OTHER TIPS

SecondActivity.Java -> I saved two edittext value in SharedPReference and called the MainActivity intent

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    Editor editor = sharedPreferences.edit();
    editor.putString("1", a.getText().toString());
    editor.putString("2", b.getText().toString());
    editor.commit();
    Intent i = new Intent(SecondActivity.this,MainActivity.class);
    SecondActivity.this.startActivity(i);
}

MainActivity.Java -> I have a button listener to show a Toast with values from the sharedpreference.

show.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            String  a = pref.getString("1", "") ;
            String b = pref.getString("2", "");
            Toast.makeText(getApplicationContext(),a+"  "+b, Toast.LENGTH_LONG).show();
        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top