java - 使用 Activity 上下文将自定义对象从 Activity 传输到 Activity ?

标签 java android android-intent android-activity serialization

问题有点复杂,首先, Activity A 和 Activity B 的 list 中都有 android:noHistory = true 。我有一个自定义 serialized 类,假设 MyClass ,该自定义类实际上通过构造函数存储 Activity B 的上下文。我在 Activity B 中有一个类型为 MyClass 的对象名称 obj ,现在我想通过 Intent 将该对象传输到 Activity C ,当 Activity B 中按下 >后退按钮。

从 Activity A 中,有一个按钮可以毫无问题地打开 Activity B,当我尝试在 onBackPressed() 中通过 B 打开 Activity C 并传输可序列化对象时,问题就开始了。我在 Activity C 中收到 NULL

[更新]我的类(class):

@SuppressWarnings("serial")
public class MyClass implements Serializable{

    private final String SHAREDKEY_HIGHSCORE = "High Scores";
    private final String FIELDKEY_HIGHSCORE = "HighScore";
    private final String FIELDKEY_HIGHTIME = "HighTime";
    private SharedPreferences sp;
    private SharedPreferences.Editor spEditor;

    public MyClass(Context context) {
        resetScore();
        sp = context.getSharedPreferences(SHAREDKEY_HIGHSCORE, Context.MODE_PRIVATE);
        spEditor = sp.edit();
    }

    public void resetScore(){
       newTime = 0;
       newScore = 0;
       highTime = 0;
       highScore = 0;
   }
}

Activity B:

public class ActivityB extends Activity {
    MyClass scores;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
        scores = new MyClass(this);
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(this, ActivityC.class);
        in.putExtra("Scores", scores);
        startActivity(intent);
    }
}

Activity C:

public class ActivityC extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_c);
        MyClass score = (MyClass) getIntent().getSerializableExtra("Scores");
        //score is null here always
    }
}

list :

<activity
    android:name=".ActivityA"
    android:noHistory="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".ActivityB"
    android:noHistory="true">

</activity>
<activity android:name=".ActivityC">

如何在 Activity C中成功接收我的自定义类(class)?请帮忙

最佳答案

问题显然就在这里//无论如何我都必须存储 Activity B 的上下文

不!您不必存储 Activity B 的上下文。您尝试这样做的原因是错误的。

在 Android 中,您绝不能尝试将 Activity 保留的时间超过其生命周期,并且绝不能尝试序列化 Context。它就是不能像这样工作,这就是它不能工作的原因。

我将其作为答案(而不是评论),因为事实就是如此。

问题的解决方案是:重新思考应用的架构。通过 Activity 传递或共享信息有几种不同的正确方法,但尝试保持上下文并将其序列化不是其中之一。

关于java - 使用 Activity 上下文将自定义对象从 Activity 传输到 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31496227/

相关文章:

Java线程同步块(synchronized block)行为-synchronized与synchronized()?

python - Android 应用程序数据库与远程数据库同步

java - 通过Android Intent发送电子邮件

android - 自定义扩展文件未在我的应用中打开

java - 有没有办法使用 j2me 以编程方式识别手机上的清除按钮?

java - 在 java TailListener 中,如何避免重复的日志消息

android - 使用 Dagger 柄作为依赖注入(inject)来处理多个改造客户端?

android - broadcastreceiver onReceive问题 ACTION_MEDIA_BUTTON Android

java - 如何解决logcat错误?

java - 将 Activity 包从 Activity 发送到 Swipe Tab 之一?