android-Recreating an Activity

本文介绍了Android中Activity状态的保存与恢复机制。当Activity因系统资源回收或用户操作被销毁时,系统会调用onSaveInstanceState()方法来保存当前状态,并在Activity重建时通过onCreate()或onRestoreInstanceState()方法恢复状态。为了确保视图状态能够正确恢复,每个视图必须拥有唯一的ID。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

when the user presses the Back button or your activity signals its own destruction by calling finish(). The system may also destroy your activity if it's currently stopped and hasn't been used in a long time or the foreground activity requires more resources so the system must shut down background processes to recover memory.

When your activity is destroyed because the user presses Backor the activity finishes itself, the system's concept of thatActivity instance is gone forever because the behavior indicates the activity is no longer needed.

By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText object). So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you.

Note: In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id attribute.

To save additional data about the activity state, you must override the onSaveInstanceState() callback method. The system calls this method when the user is leaving your activity and passes it the Bundle object that will be saved in the event that your activity is destroyed unexpectedly. If the system must recreate the activity instance later, it passes the same Bundle object to both the onRestoreInstanceState() andonCreate() methods.

static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
    
    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first
   
    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}
public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);
   
    // Restore state members from saved instance
    mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
    mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}
Caution:  Always call the superclass implementation of  onRestoreInstanceState()  so the default implementation can restore the state of the view hierarchy.

CH341A编程器是一款广泛应用的通用编程设备,尤其在电子工程和嵌入式系统开发领域中,它被用来烧录各种类型的微控制器、存储器和其他IC芯片。这款编程器的最新版本为1.3,它的一个显著特点是增加了对25Q256等32M芯片的支持。 25Q256是一种串行EEPROM(电可擦可编程只读存储器)芯片,通常用于存储程序代码、配置数据或其他非易失性信息。32M在这里指的是存储容量,即该芯片可以存储32兆位(Mbit)的数据,换算成字节数就是4MB。这种大容量的存储器在许多嵌入式系统中都有应用,例如汽车电子、工业控制、消费电子设备等。 CH341A编程器的1.3版更新,意味着它可以与更多的芯片型号兼容,特别是针对32M容量的芯片进行了优化,提高了编程效率和稳定性。26系列芯片通常指的是Microchip公司的25系列SPI(串行外围接口)EEPROM产品线,这些芯片广泛应用于各种需要小体积、低功耗和非易失性存储的应用场景。 全功能版的CH341A编程器不仅支持25Q256,还支持其他大容量芯片,这意味着它具有广泛的兼容性,能够满足不同项目的需求。这包括但不限于微控制器、EPROM、EEPROM、闪存、逻辑门电路等多种类型芯片的编程。 使用CH341A编程器进行编程操作时,首先需要将设备通过USB连接到计算机,然后安装相应的驱动程序和编程软件。在本例中,压缩包中的"CH341A_1.30"很可能是编程软件的安装程序。安装后,用户可以通过软件界面选择需要编程的芯片类型,加载待烧录的固件或数据,然后执行编程操作。编程过程中需要注意的是,确保正确设置芯片的电压、时钟频率等参数,以防止损坏芯片。 CH341A编程器1.3版是面向电子爱好者和专业工程师的一款实用工具,其强大的兼容性和易用性使其在众多编程器中脱颖而出。对于需要处理25Q256等32M芯片的项目,或者26系列芯片的编程工作,CH341A编程器是理想的选择。通过持续的软件更新和升级,它保持了与现代电子技术同步,确保用户能方便地对各种芯片进行编程和调试。
### Locale-Emulator 报错解决方案 在使用 Locale-Emulator 时,可能会遇到各种错误情况。以下是常见的错误及其对应的解决方案: #### 1. **无法模拟区域设置** 如果发现 Locale-Emulator 未能成功更改设备的语言或地区设置,可能是由于权限不足或其他配置问题引起的。可以通过以下方式解决: - 确保已授予应用所需的权限,例如 `WRITE_SETTINGS` 或者 `SYSTEM_ALERT_WINDOW` 权限[^1]。 - 如果是在 Android 10 及以上版本运行,可能需要额外启用开发者选项中的“允许调试 APK”功能。 #### 2. **ADB 命令执行失败** 当尝试通过 ADB 控制 Locale-Emulator 的行为时,可能出现命令执行失败的情况。例如,在停止 QQ 应用的过程中出现问题: ```bash [D:\android-sdk-windows\platform-tools\adb.exe -P 5037 -s emulator-5554 shell am force-stop com.tencent.mobileqqi] ``` 这通常是因为目标设备未连接、包名拼写错误或者服务不可用所致[^2]。可以按照以下步骤排查: - 验证设备是否正常连接并处于可调试状态:`adb devices`。 - 检查指定的应用包名是否正确。 - 尝试重启 ADB 服务器:`adb kill-server && adb start-server`。 #### 3. **窗口管理异常** 某些情况下,Locale-Emulator 可能会抛出类似于以下的错误: ```plaintext android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application ``` 这种问题是由于窗口令牌无效造成的。具体原因和修复方法如下: - 错误通常是由于上下文不匹配引起,比如使用了 `getApplicationContext()` 而非 Activity 上下文[^3]。 - 修改代码逻辑,将 `getApplication()` 替换为当前 Activity 实例(即 `this`),从而确保正确的上下文传递。 #### 4. **广播接收器注册问题** 如果 Locale-Emulator 使用了动态广播接收器,则可能存在未注销广播接收器的风险。典型表现为日志中提示类似以下内容: ```plaintext call you are unregister onbrocastRevicer? ``` 这是由于忘记调用 `unregisterReceiver()` 方法导致的资源泄漏问题[^3]。建议在生命周期结束前显式注销广播接收器。 #### 5. **更新冲突** 安装过程中出现以下错误信息: ```plaintext INSTALLATION error: INSTALL_FAILED_UPDATE_INCOMPATIBLE ``` 该问题表明旧版应用尚未完全卸载就试图覆盖安装新版本[^3]。推荐的操作流程包括: - 手动删除现有应用实例后再重新部署。 - 清理缓存数据以及关联文件夹路径下的残留记录。 #### 6. **R.java 文件丢失** 开发阶段偶尔会发生 R.java 自动移除的现象,并伴随重建需求的通知: ```plaintext R.java was removed! Recreating R.java! ``` 此类警告一般不会影响最终产物质量,但仍需注意保持构建环境稳定性和依赖同步准确性[^3]。 --- ### 示例代码片段 下面提供一段简单的脚本用于验证 Locale-Emulator 是否能够切换至特定语言环境: ```java import android.content.Context; import android.os.LocaleList; public class LocaleHelper { public static void setAppLocale(Context context, String languageCode) { LocaleList localeList = new LocaleList(new Locale(languageCode)); LocaleList.setDefault(localeList); Configuration config = context.getResources().getConfiguration(); config.setLocales(localeList); context.createConfigurationContext(config); // Apply changes immediately. } } ``` --- ### 总结 针对 Locale-Emulator 中常见的一些报错现象进行了分析说明,并给出了相应的调整措施。实际操作时还需结合具体情况灵活应对。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值