简体   繁体   English

如何将String从2个活动传递到一个活动?

[英]How to pass String from 2 activities to one?

I have two activitys with lists. 我有两个清单活动。

One for tablet's and one for regular density phones. 一种用于平板电脑,另一种用于普通密度电话。 When an item in the list is clicked it launches the same activity for either activity with list's. 单击列表中的项目时,它会为具有列表的任一活动启动相同的活动。

The problem is when an item is clicked i have getter and setter class that gets the URL for a particular item and passes it to the launching activity like this... 问题是当单击一个项目时,我有一个getter和setter类,它获取特定项目的URL并将其传递给这样的启动活动...

    private String URL = null;
try{
        URL = com.fttech.AbstractFeedsActivity.feed_url;

    }
    catch(IllegalArgumentException e){
        e.printStackTrace();
        URL = com.fttech.ItemsActivity.url;

    }

As you see what i tried to do is try and catch. 如您所见,我试图做的就是抓住。 So if the first one isnt found then the second one will be retrieved. 因此,如果找不到第一个,则将检索第二个。

But it doesnt seem to work. 但这似乎不起作用。

It returns null each time. 每次返回null。

For what i have describe what is the best way to implement this? 我所描述的是实现此目标的最佳方法是什么? Is my way logic? 我的方式合乎逻辑吗? Or is there a better way. 或者,还有更好的方法。

Thanks 谢谢

Try this, 尝试这个,

     private String URL = null;
     try{
            URL = com.fttech.AbstractFeedsActivity.feed_url;
            if(TextUtils.isEmpty(URL)){
                 URL = com.fttech.ItemsActivity.url;
                 // Pass this URL   
            }
            else{
              // If its not empty then it will pass the first URL
          }
        }
        catch(IllegalArgumentException e){
            e.printStackTrace();
        }

无需将String传递给2个活动,如果您只想将字符串传递给一个或任意多个活动,只需将它们放在SharedPreferences中或在静态类中声明一个变量,然后在需要时进行设置/获取即可。

Passing values from one Activity to another: 将值从一个活动传递到另一个:

Intent intent = new Intent(context, CalledActivity.class);
intent.putExtra(key, value);
startActivity(intent);

If you want some data back from called Activity then you can use startActivityForResult() as: 如果要从调用的活动中返回一些数据,则可以将startActivityForResult()用作:

Intent intent = new Intent(context, CalledActivity.class);
intent.putExtra(key, value);
startActivityForResult(intent, requestCode);

In called activity you can set data as: 在被叫活动中,您可以将数据设置为:

setResult(RESULT_OK, intent);

Note: Here you set the value in intent and pass it to setResult(). 注意:在这里,您可以按意图设置值,并将其传递给setResult()。

On returning back to calling Activity you can get data by overriding: 返回调用Activity时,您可以通过覆盖以下内容获取数据:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){
        //Get data from Intent "data" and do your task here....
    }
}

Note: You can pass primitive data type values thru Intent and if you want to pass other types then you have to use Bundle like this. 注意:您可以通过Intent传递原始数据类型值,如果要传递其他类型,则必须使用Bundle这样。

Bundle data = new Bundle();
data.putIntArray(key, value);

//same way you can set other values....... //Now set this Bundle value to Intent as you do for primitive type.... //以同样的方式可以设置其他值.... //现在像使用原始类型一样,将此Bundle值设置为Intent。

Intent intent = new Intent(context, CalledActivity.class);
intent.putExtra(data);
startActivity(intent);

Receiving data in Activity: 在活动中接收数据:

//For primitive values: //对于原始值:

DataType var_name = getIntent().getExtras().get(key);

//For Bundle values: //对于Bundle值:

Bundle var_name = getIntent().getExtras().getBundle(key);

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

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