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

调用外部Intent时savedInstanceState引用错误值的Android技术求助

Hey there! Let's work through this savedInstanceState issue you're hitting when launching external Intents. I've been in your shoes as an Android beginner, so let's break this down step by step.

First, Let's Diagnose the Likely Causes

When you launch an external Intent (like opening the camera or a browser), your Activity2 gets put into the background, and the system might destroy it to free up resources. When you return, it gets recreated, and that's where savedInstanceState is supposed to restore your latest state. The "wrong values" usually come from one of these common mistakes:

  1. Mismatched key names between saving and restoring the values
  2. Not prioritizing savedInstanceState over the original Intent Bundle when recreating the activity
  3. Saving outdated initial values instead of the updated counts from Activity2

Let's Fix This with Concrete Steps

1. Standardize Your Key Names

First, make sure the keys you use to save values in onSaveInstanceState match exactly what you use to retrieve them. From your code snippet, it looks like you might be mixing variable names (e.g., passCount vs nPassed). Stick to consistent names to avoid typos.

2. Correctly Restore State in onCreate

When Activity2 is recreated after an external Intent, the original Intent's Bundle will still have the initial values from Activity1—not the updated counts you changed in Activity2. So you need to check if savedInstanceState exists first, and use that to restore your latest state.

Here's how to adjust your onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_2);

    // Initialize your count variables
    int passCount, failCount, skipCount;

    if (savedInstanceState != null) {
        // Activity is being recreated - use saved state
        passCount = savedInstanceState.getInt("passCount");
        failCount = savedInstanceState.getInt("failCount");
        skipCount = savedInstanceState.getInt("skipCount");
    } else {
        // First time launching - get initial values from Activity1's Bundle
        Bundle intentBundle = getIntent().getExtras();
        if (intentBundle != null) {
            passCount = intentBundle.getInt("passCount");
            failCount = intentBundle.getInt("failCount");
            skipCount = intentBundle.getInt("skipCount");
        } else {
            // Fallback default values if Bundle is missing
            passCount = 0;
            failCount = 0;
            skipCount = 0;
        }
    }

    // Use these counts for your UI or logic here
}

3. Save the Latest Values in onSaveInstanceState

Make sure you're saving the current, updated values of your counts—not the initial ones from Activity1. Double-check that you're referencing the right variables in your save method:

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    // Save the CURRENT values of your counts, not initial ones
    savedInstanceState.putInt("passCount", passCount);
    savedInstanceState.putInt("failCount", failCount);
    savedInstanceState.putInt("skipCount", skipCount);
}

Quick Check to Verify

  • If you update passCount in Activity2 (e.g., via a button click), does the variable actually hold the new value before onSaveInstanceState is called? You can add a log statement to confirm:
    Log.d("Activity2", "Saving passCount: " + passCount);
    
  • Ensure you're not accidentally using a different variable name (like nPassed instead of passCount) in the save method—this is a super common typo!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:22:37