You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在后台便捷杀死Activity/进程以测试UI数据恢复功能?

Got it, when you're running UI state restoration tests, being able to quickly kill activities or processes in the background is key to reliably reproducing scenarios and verifying if your app can recover data correctly. Below are the most practical methods I rely on:

方法一:使用ADB命令(适合自动化/精准测试)

This is my go-to for both manual testing and scripting. You'll need ADB set up on your computer and your device connected via USB debugging.

  • 杀死指定Activity
    First, grab the full class name of your target Activity while it's active with this command:

    adb shell dumpsys window | grep mCurrentFocus
    

    Look for a line like mCurrentFocus=Window{xxxx u0 com.your.package/com.your.package.YourActivity} — the segment after the last slash is your Activity class name.

    Then kill it directly:

    adb shell am force-stop com.your.package/com.your.package.YourActivity
    
  • 杀死整个应用进程
    If you want to wipe the entire app from memory, use this command (you can get your app's package name via adb shell pm list packages or from your build.gradle file):

    adb shell am force-stop com.your.package
    

    For a more aggressive kill (simulating a system-initiated process termination):

    adb shell kill -9 $(adb shell pidof com.your.package)
    
方法二:利用Android开发者选项(无需电脑,适合快速手动测试)

Perfect for on-the-go testing without a computer attached.

  1. First, enable Developer Options: Go to Settings > About Phone and tap Build Number 7 times until you see the "You're now a developer!" prompt.
  2. Head back to Settings and open Developer Options.
  3. Use one of these two options:
    • 不保留活动 (Don't keep activities): Toggle this on. Now, as soon as you leave an Activity (press home or switch apps), the system will immediately destroy it. Reopen your app to check if the UI state is restored.
    • 后台进程限制 (Background process limit): Set this to No background processes. Any app not in the foreground will have its processes killed right away. Switch away from your app and return to test state restoration.
测试小技巧
  • Before killing the Activity/process, add specific test data: fill out a form field, scroll to a specific list position, or open a detail page. This makes it easy to confirm if the state is properly recovered.
  • For edge cases, try killing the app while it's performing a background task (like saving data) to ensure your state handling is robust.

内容的提问来源于stack exchange,提问作者Jim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 00:22:34