-1

我根本没有找到在活动之间共享实时数据的解决方案。我的第一个活动接收实时对象(双精度型,随机数)。我想把这个数字传递给第二个活动。一切正常,只有第二个 Activity 只显示一次数据。我必须通过返回第一个活动来刷新活动,然后第二个活动才会显示最新数据。我实现了一个单格顿模式:

public class FirstActivity extends Activity{
 public double xAxis;
 public double yAxis;
 public static FirstView instance;

     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.device_view);

        handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    int data1 = msg.arg1;
                    xAxis = (double) data1;
                    dataX.setText(String.valueOf(xAxis));
                    int data2 = msg.arg2;
                    yAxis = (double) data2;
                    dataY.setText(String.valueOf(yAxis));

                }
            };
        secondview.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent nextScreen = new Intent(getApplicationContext(),
                            SecondActivity.class);
                    startActivity(nextScreen);
                }
            });

    }

     public static void initInstance(){
    if(instance == null)
    {
       instance = new FirstActivity();
    }
     }

    public static FirstActivity getInstance(){
        return instance;
    }
}

第二视图类

public class SecondActivity extends Activity{
       private double valueX;
    private double valueY;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linegraph); 
        valueX = FirstActivity.getInstance().xAxis;
        valueY = FirstActivity.getInstance().yAxis;
      }
}

应用类

package com.bluetoothcomm;

import android.app.Application;

public class MyApplication extends Application {

    @Override
    public void onCreate(){
        super.onCreate();
        initSingeltons();
    }

    public void initSingeltons(){
        FirstActivity.initInstance();
    }

}
4

4 回答 4

1

我发现了我的问题,解决方案是使用Application. 只有这个剂量不能解决我的问题。问题是静态变量实例public static FirstView instance绑定到类加载器,第一个初始化它的类。因此,当任何类中的静态变量已被 Activity 初始化时,当第二个 Activity 启动时,第一个 Activity 被销毁,这意味着静态变量也未初始化。这就是为什么 SecondActivity 没有获取最新数据或实时数据的原因,它只捕获静态常量数据。

我用 Singelton 和 Application 的组合稍微改变了我的代码,因为这样在激活 SecondActivity 时静态变量永远不会被取消初始化。但是我仍然得到相同的结果,当我切换到第二个活动时,静态变量实例未初始化。我做错了什么,有没有人看到。我添加了我的代码。

@Max Rasguido,@Orabig

于 2012-08-26T20:20:05.757 回答
1

You may implement a background service capable of providing the real time data to Activity1 and also to Activity2. I am guessing that your problem ocurrs if you are passing data from Activity1 to Activity2 through an Intent with putExtras, on this way it will only do this at the moment you start Activity2.

于 2012-08-25T23:54:34.247 回答
0

您应该使用意图过程。文档

于 2012-08-25T23:41:24.950 回答
0

How is your data supposed to change when activity2 is shown, if you say that it's received by activity1 ?

However, I would use a preference, or an attribute of your application class (which is a singleton itself), but you give too little informations to fully understand your needs...

于 2012-08-25T23:50:23.833 回答