保存和恢复活动状态时,活动由$ P $用户pssing后退按钮关闭 [英] Saving and restoring Activity State when Activity is closed by pressing back button by user

查看:388
本文介绍了保存和恢复活动状态时,活动由$ P $用户pssing后退按钮关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序以收集客户详细资料如个人,业务,资产等。

I am working on an App for collecting Customer Details like personal, business, assets etc.

该应用程序显示了的ListView 的选项,并根据活动启动选项。个人用户在输入信息presses后退按钮,返回到MainActivity(活动的ListView ),并选择后另一种选择喜欢的公司详细信息和资产的细节。用户输入的细节和后退按钮回来MainActivity并选择其它选项。

The app shows a ListView with options and based on options Activities are started. After entering details in personal user presses back button and returns to MainActivity (Activity with a ListView) and selects another option like business details and assets detail. User enters the details and come back to MainActivity by Back button and selects another option.

我想救我的活动 S使用 onSavedInstance(捆绑) onRestoreInstanceState(束)和共享preference但失败了。

I tried to save my Activitys using onSavedInstance(Bundle) and onRestoreInstanceState(Bundle) and sharedpreference but failed.

谁能帮助?如果需要,我会提供code。

Can anyone help? I will provide code if required.

推荐答案

的onSaveInstanceState() / OnRestoresInstanceState()是不应该在从活动pssed一个返回键$ p $使用。事实上,的onSaveInstanceState()永远不会被调用。在这里,用户被明确破坏活动,并从后面堆栈中删除。

OnSaveInstanceState()/OnRestoresInstanceState() is not supposed to be used on a back key pressed from an Activity. In fact , OnSaveInstanceState() is never called. Here the user is explicitly destroying the Activity and removing it from the back stack.

这两种方法在用户presses home键或如Android系统破坏你的活动情况下使用。

These two methods are used in case where user presses home button or in cases where Android System destroys your Activity.

选项,你可以尝试


  1. 尝试使用 StartActivityForResult() / 的onActivityResult()方法。在这里,目标活动可以设置whcih可以通过您的父活动进行访问的数据。

  2. 使用(用于保存数据单一实例类(单件类))的应用程序类。

  3. 如共享共享preferences事情,文件,数据库等。

  1. Try using StartActivityForResult()/OnActivityResult() methods. Here the target activity can set data whcih can be accessed by your parent activity.
  2. Use an application class(a single instance class (singleton class) for holding data).
  3. Things like shared SharedPreferences, files, database, etc.

这篇关于保存和恢复活动状态时,活动由$ P $用户pssing后退按钮关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆