简体   繁体   中英

how to use OnPause method

How to change button text loaded from EditText while in onPause() method?

For example

@Override
public void onPause(){
    button.setText(text.getText().toString());
    super.onPause()
}

When I press the back button it does not change the button text

Leaving aside the question of why you want to do this in onPause() in the first place...

Normally, the state of your views would be saved in onSaveInstanceState(...) . But according to the docs , there are no guarantees about whether the call to onSaveInstanceState(...) will occur before or after the call to onPause() . If onSaveInstanceState(...) is called before onPause() , then any changes you make in onPause() will be lost.

Please read documentation - it describes Android activity flow with excellent diagrams.
onPause() is already existing method in Your activity, thus You will need to @Override it. Unless You modified back button behavior - here is what happens:

  • onPause() is executed when You press back, and button text is replaced (assuming text and button are visible in this scope) - for such short time, You won't be able to notice (unless device is lagging, anyway)
  • onStop() is executed next, and Your application exits shortly after

If You want to try and check that text is properly set - You might find it easier to write:

@Override
public void onPause(){
    super.onPause();
    Log.d("Text's text:" + text.getText().toString()
          + "Button:" + button.getText().toString());

}

You will need to import android.util.Log .
Notice also, that it is highly recommended ( here ) that You call super() as first thing when overriding Android default methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM