简体   繁体   中英

Saving the state of an activity before pressing the back button

I am currently building an android app which requires saving the state of my variables and views when the back button is pressed. I know the onSaveInstanceState() would work for scenarios which involve change in orientation but how do I get it to save when I use the back button.

You may override finish() method and do saving there. This method is called when the activity is finished — that means, it's removed from activity stack. By default this happens when you press back button.

You can override,

@Override
public void onBackPressed() {
super.onBackPressed();
}

and save the state in Shared Preferences.

正如Sarge所说,您可以重写onFinish()并将数据保存在首选项,AppDelegate等中。或者您可以保存数据并将其发送到上一个ActivityResult的活动中,或者可以尝试找到适当的活动标志,以便此操作活动不会被杀死。

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