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

跨Activity传递的数据未保存,求排查错误原因及解决方案

Hey there! Let's figure out why your data isn't sticking when passing between Activities. From the code snippet you shared, it looks like you started setting up an Intent and Bundle but didn't finish—let's walk through the most common mistakes that cause this issue, along with fixes:

Common Issues & Fixes for Activity Data Transfer Failures

1. Incomplete Bundle & Intent Setup

Your code cuts off at Bundl..., so first make sure you're properly creating a Bundle, adding your data, attaching it to the Intent, and starting the target Activity. Here's the full, correct pattern:

final Button nameActivity = (Button) findViewById(R.id.nameActivity);
nameActivity.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        userFirstName = firstName.getText().toString();
        userLastName = lastName.getText().toString();
        
        Intent nameIntent = new Intent(MainActivity.this, NameActivity.class);
        // Create and populate the Bundle
        Bundle nameBundle = new Bundle();
        nameBundle.putString("KEY_FIRST_NAME", userFirstName);
        nameBundle.putString("KEY_LAST_NAME", userLastName);
        
        // Attach Bundle to the Intent
        nameIntent.putExtras(nameBundle);
        // Don't forget to launch the Activity!
        startActivity(nameIntent);
    }
});

Pro tip: Use constant string keys (like KEY_FIRST_NAME) instead of hardcoded strings—this avoids typos that break data retrieval later.

2. Incorrect Data Retrieval in NameActivity

Even if you send data correctly, you might be fetching it wrong in the target Activity. Double-check your onCreate method there:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_name);
    
    // Retrieve the Bundle from the Intent
    Bundle receivedBundle = getIntent().getExtras();
    if (receivedBundle != null) {
        // Use the SAME keys you used to put the data in!
        String fetchedFirstName = receivedBundle.getString("KEY_FIRST_NAME");
        String fetchedLastName = receivedBundle.getString("KEY_LAST_NAME");
        // Now you can use these values (e.g., display them in TextViews)
    }
}

Always check if the Bundle is null—this prevents crashes and lets you confirm if data is coming through at all.

3. Typos in Key Names

This is the #1 culprit! If the key you use to add data to the Bundle doesn't exactly match the key you use to retrieve it, you'll get null values. For example, using "first_name" when putting and "FirstName" when getting will break things. Sticking to constant keys (defined as public static final String in your Activity) eliminates this risk.

4. Data Loss During Configuration Changes

If your NameActivity gets recreated (like when rotating the screen), the Intent extras are actually preserved by default—but if you're storing the data in a variable that gets reinitialized every time onCreate runs, make sure you re-fetch it from the Intent each time, not just once.

If You Still Have Issues

If you can share the full code for both the click listener in MainActivity and the onCreate method in NameActivity, we can pinpoint the exact mistake. But based on common pitfalls, the above fixes should resolve most cases where data isn't being passed correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:14:58