android - 如何发送带有 putextra Intent 的 android.location.Address

标签 android android-intent static bundle start-activity

是否可以将带有Intentandroid.location.Address 实例放到另一个 Activity 中?

Address current_location;

我的意思是,我试图通过以下方式将 current_location 直接放在 Intent 中:

  1. IntentName.putExtra("current_location",current_location);

  2. 或通过 bundle 间接:

    Bundle b = new Bundle();
    b.put....("current_location",current_location);
    IntentName.put(b);
    

如果以上策略都不支持“地址”,那么将地址实例传递给另一个 Activity 的最佳方法应该是什么? 如果我声明, public static Address current_location;

并尝试从另一个 Activity 访问它,那么无法保证 Android 将保存 current_location 数据。还是我错了?

在这种情况下,应该采用什么方法?

最佳答案

您需要将其作为可打包文件传递。通过查看API,我认为它们已经符合标准。

地址 扩展对象 实现 Parcelable

https://developer.android.com/reference/android/location/Address.html

所以要打包并发送它,您可以这样做:

    String key = getResources().getString(key);

    startActivity( new Intent()
                        .setClass(this, YourActivity.class)
                        .putExtra(key, current_location )
    );

然后在您的 Activity 中重新组装它:

   String key = getResources().getString(key);
   Address current_location = getIntent().getExtras().getParcelable(key); 

key 就是您用来存储 parcelable 的名称。

关于android - 如何发送带有 putextra Intent 的 android.location.Address,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11214829/

相关文章:

android - 当我在地址栏中输入 "myapp://"时,如何从 Android 浏览器启动应用程序

c# - 静态和扩展方法策略

excel - 是否可以使用硬编码的值列表而不是范围公式中的范围?

docker - 将静态文件从Docker容器复制到非空命名卷

android - 如何解析四方类别json

android - NoClassDefFoundError 与 EclEmma 代码覆盖工具

java - 通过 NavigationComponent 在 BottomNavigation fragment 之间传递数据

安卓 Intent 数据库

Android 7 中的 Androidx 导航图标兼容性问题

java - 如何解决Intent引起的空指针异常