简体   繁体   English

在 Android 中将变量从父视图传递给子视图

[英]Passing variables from Parent to Child views in Android

Whats the difference between pass the variables like this:像这样传递变量有什么区别:

Intent intent = new Intent(mCtx, DetailsActivity.class);
intent.putExtra("pId", id);
intent.putExtra("pType", type);

mCtx.startActivity(intent);

and using the keyword Bundle ?并使用关键字Bundle

Intent intent = new Intent(MainActivity.this, DetailsActivity.class);

// Now let's Pass data using Bundle
Bundle bundle = new Bundle();
bundle.putString("pId", id);
bundle.putString("pType", type;
intent.putExtras(bundle);

startActivity(intent);

Im new to Android development and I am curious to which method is better or stadard when doing android development?我是 Android 开发的新手,我很好奇在进行 android 开发时哪种方法更好或标准?

Intent意图

public Intent putExtra(String name, String value) { 
  if (mExtras == null) { 
        mExtras = new Bundle(); 
     } 
    mExtras.putString(name, value); 
    return this; 
}

Bundle

public void putString(String key, String value) { 
    unparcel(); 
     mMap.put(key, value);
 }

putExtras does not put your bundle inside Intent. putExtras 不会将您的包放入 Intent。 Instead, it copies it over to the current intent bundle.相反,它将它复制到当前的意图包中。

public Intent putExtras(Bundle extras) { 
    if (mExtras == null) { 
       mExtras = new Bundle(); 
    } 
    mExtras.putAll(extras); 
    return this; 
}

The first method is recommended.推荐第一种方法。 All samples are using the first putExtra (key, value) methods.所有示例都使用第一个 putExtra(key, value) 方法。

In most scenarios you should know what you are passing to the next activity, so putExtra ( key, value ) makes sense because you know what the key is.在大多数情况下,您应该知道要传递给下一个活动的内容,因此 putExtra ( key, value ) 很有意义,因为您知道键是什么。

The only scenario I can think of to use the second method is a transient activity which receives bundle from previous activity and need to pass all information to next activity.我能想到的使用第二种方法的唯一场景是一个瞬态活动,它从前一个活动接收包并且需要将所有信息传递给下一个活动。 The transient activity does not need to know what is being passed and just pass everything it received to the next activity.瞬态活动不需要知道正在传递什么,只需将它接收到的所有内容传递给下一个活动。

BTW, if you create your own Bundle, put the key values in and then call putExtra ( bundle), there is extra cost for the temporary bundle so it is less efficient.顺便说一句,如果您创建自己的 Bundle,将键值放入,然后调用 putExtra (bundle),临时 bundle 会产生额外成本,因此效率较低。

Whats the difference between pass the variables like this and using the keyword Bundle?像这样传递变量和使用关键字 Bundle 有什么区别?

One puts the keys directly and one puts them in a Bundle first.一个是直接把钥匙放在一个包里,一个是先把它们放在一个 Bundle 中。 The end result is the same.最终结果是一样的。

Im new to Android development and I am curious to which method is better or stadard when doing android development?我是 Android 开发的新手,我很好奇在进行 android 开发时哪种方法更好或标准?

The first is "better" or more "standard" simply because it eliminates the need to create the bundle yourself first - it's a redundant step.第一个是“更好”或更“标准”,因为它消除了首先自己创建包的需要——这是一个多余的步骤。 You'd really only ever need to use that method if your code already had some logic that maintained a Bundle of data that you wanted to pass along in an Intent as key / value pairs.如果您的代码已经有一些逻辑来维护您希望在Intent作为键/值对传递的数据Bundle ,那么您实际上只需要使用该方法。

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

相关问题 android将事件从子视图传递到父视图 - android passing events from child view to parent 从在 Android 中以编程方式创建的父视图中查找子视图 - Finding child views from parent view created programmatically in Android 数据从父片段传递到选项卡式片段(子)-Android - Data Passing from Parent Fragment to Tabbed Fragment (child) - Android Android:将数据从子片段传递到父片段 - Android: Passing data from child fragment to parent fragment 将可见性从父级视图传播到子级视图 - Propagate the visibility from parent to child views 基于父适配器的值,an​​droid中的ExpandableListView是否可能具有不同的子视图? - Is it possible to have different child views for an ExpandableListView in android based on a value from the parent adapter? 当父母在Android中被解雇时,子视图会如何处理 - What happens to child views when parent is dismissed in Android 父视图和子视图可以在Android中同时获取运动事件吗? - Can parent and child views obtain the motion event simultaneously in Android? Android-使用ExpandableListView在父视图内渲染子视图 - Android - Render child views inside parent view with ExpandableListView 同时为父视图和子视图检测Android-Touch事件 - Android-Touch event detection for both parent and child views simultaneously
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM