简体   繁体   English

动态putExtra()和getExtra()

[英]Dynamic putExtra() and getExtra()

I have 3 activities : 1 main activity with some methods to calculate xy and z positions, and the 3 activities are floors which will switch depending on the value of z. 我有3个活动:1个主要活动,其中一些方法可以计算xy和z位置,这3个活动是根据z的值切换的楼层。

When I go from activity A (main activity) to B it seems like the datas i've used saved with putExtra and getExtra are frozen to their initial value (ie at the 1st putExtra). 当我从活动A(主要活动)转到B时,我使用putExtra和getExtra保存的数据似乎被冻结为其初始值(即,在第一个putExtra处)。 Is it normal ? 正常吗? If so do you have any idea as to how I should access dynamically x, y, z from another activity? 如果是这样,您是否对我应该如何从另一个活动动态访问x,y,z有任何想法?

In the code i'll show you x,y and z are calculated in the loop that's just for testing. 在代码中,我将向您显示x,y和z是在仅用于测试的循环中计算的。 Otherwise I have other methods to calculate them (using WiFi RSSI and a Gradient Descent algorithm) 否则,我还有其他方法来计算它们(使用WiFi RSSI和Gradient Descent算法)

MainActivity 主要活动

    public class MainActivity extends AppCompatActivity {
           ImageView myImageView;
           int i = 0;
           int z = 1;
           int x,y,f=1;
           final int imageWidth = 930;
           final int imageHeight = 560;
           private Timer timer;
           int flag_activity=1;

[....] Intent Initialisation [....]意图初始化

    final Intent intent0 = new android.content.Intent(this, Rdc.class);
    final Intent intent2 = new android.content.Intent(this, r2.class);
    intent0.putExtra("X", i);
    intent0.putExtra("Y", i);
    intent0.putExtra("Z", f);

    intent2.putExtra("X", i);
    intent2.putExtra("Y", i);
    intent2.putExtra("Z", f);


    myImageView = (ImageView) findViewById(R.id.imageView);
    VectorDrawableCompat vectorDrawable = VectorDrawableCompat.create(getResources(), R.drawable.ic_android_black_24dp, null);
    myImageView.setImageDrawable(vectorDrawable);

//.... // ....

Loop to update image and change activities depending on the value of f: 循环更新图像并根据f的值更改活动:

    Timer T=new Timer();
    T.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {
                    i++;
                    i=i%10;
                    if(i==7) {f++; f=f%3;}
                    updateTextView(i,i,f);


                    if(f==1) {
                        myImageView.setTranslationX(i * imageWidth / 10);
                        myImageView.setTranslationY(i * imageHeight / 10);
                        flag_activity=1;
                    }
                    else {
                        if(f==2&&flag_activity!=2) {startActivity(intent2); flag_activity=2;}
                        else {
                            if(f==0&&flag_activity!=0) {startActivity(intent0); flag_activity=0;}
                        }                        }


                }
            });
        }
    }, 1000, 1000);

Another activity (the 2 other are the same aside from some resources). 另一个活动 (除某些资源外,其他两个活动相同)。

public class r2 extends AppCompatActivity {
ImageView myImageView;
int imageWidth=930;
int imageHeight=560;
int x,y,z;


public void updateTextView(int a, int b, int c) {
    String str="x :"+Integer.toString(a)+"y :"+Integer.toString(b)+"f :"+Integer.toString(c);
    TextView textView = (TextView) findViewById(R.id.textView3);
    textView.setText(str);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_r2);

    myImageView = (ImageView) findViewById(R.id.imageView);
    VectorDrawableCompat vectorDrawable = VectorDrawableCompat.create(getResources(), R.drawable.ic_android_black_24dp, null);
    myImageView.setImageDrawable(vectorDrawable);


    Timer T2=new Timer();
    T2.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {

                    x = getIntent().getIntExtra("X",1);
                    y = getIntent().getIntExtra("Y",1);
                    z = getIntent().getIntExtra("Z",1);
                    updateTextView(x,y,z);

                    if(z==2) {

                        myImageView.setTranslationX(x * imageWidth / 10);
                        myImageView.setTranslationY(y * imageHeight / 10);
                    }
                    else {
                        finish();
                    }
                }
            });
        }
    }, 1000, 1000);



}

} }

Not sure how you are putting the extras in the intent. 不知道您打算如何使用这些功能。 But I will show you how you should do it 但我会告诉你你应该怎么做

Intent intentForActivityB = new Intent(this,ActivityB.class);
intentForActivityB.putExtra("YourKey","Your data")
startActivity(intentForActivityB);

Inside ActivityB
intent.getIntExtra("YourKey",defaultValue);

So maybe in your case, the data is not frozen its just giving you default value. 因此,也许在您的情况下,数据没有冻结,只是为您提供了默认值。

It's better to use bundle when you try to pass multiple data to another activity. 尝试将多个数据传递到另一个活动时,最好使用捆绑软件。

First create a bundle object 首先创建一个捆绑对象

Bundle bundle = new Bundle();
bundle.putString("key1", "string data");
bundle.putInt("key2",12345);

Then pass your bundle with intent. 然后有目的地传递您的捆绑软件。 Be careful you will have to use putExtras not putExtra 注意,您必须使用putExtras而不是putExtra

Intent intent = new Intent(context, destinationClass);
intent.putExtras(bundle);

and then start the intent 然后开始意图

context.startActivity(intent);

On the destination class you can receive the bundle with getExtras 在目标类上,您可以使用getExtras接收捆绑包

Bundle bundle =getIntent().getExtras();

And from that bundle you can extract your data 然后从该捆绑包中提取数据

String yourStringData = 
bundle.getString("key1");
int youtIntData = bundle.getInt("key2");

Bundle supports many data types. 捆绑软件支持许多数据类型。 Please check the bundle documentation for more details 请查看捆绑包文档以了解更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM