是否可以将带有Intent
的android.location.Address
实例放到另一个 Activity 中?
Address current_location;
我的意思是,我试图通过以下方式将 current_location 直接放在 Intent 中:
IntentName.putExtra("current_location",current_location);
或通过 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/