博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Handling bundles in activities and fragments
阅读量:5132 次
发布时间:2019-06-13

本文共 6165 字,大约阅读时间需要 20 分钟。



is a useful data holder, which maps String values to various types. So basically it is a heterogenous key/value map. Bundles are used in , and for transporting data. I would like to describe how I work with Bundles on Android and show you some good tips.

Activity

When you are creating a new instance of Activity via Intent, you can pass some extra data to the Activity. Data are stored in Bundle and can be retrieved by calling getIntent().getExtras(). It's a good practise to implement static method newIntent() which returns a new Intent that can be used to start the Activity. This way you have compile time checking for the arguments passed to the Activity. This pattern is suitable for Service and Broadcast as well.

Bundle is also used if the Activity is being re-initialized (e.g. because of configuration changes) for keeping the current state of the instance. You can supply some data in onSaveInstanceState(Bundle) and retrieve them back in onCreate(Bundle) method or onRestoreInstanceState(Bundle). Main difference between these methods is that onRestoreInstanceState(Bundle) is called after onStart(). Sometimes it's convenient to restore data here after all of the initialization has been done. Another purpose could be allowing subclasses to decide whether to use your default implementation.

See example code below. Note that EXTRA_PRODUCT_ID constant is public. That's because we could need it in a Fragment encapsulated in this Activity.

public class ExampleActivity extends Activity{
public static final String EXTRA_PRODUCT_ID = "product_id"; public static final String EXTRA_PRODUCT_TITLE = "product_title"; private static final String SAVED_PAGER_POSITION = "pager_position"; public static Intent newIntent(Context context, String productId, String productTitle) {
Intent intent = new Intent(context, ExampleActivity.class); // extras intent.putExtra(EXTRA_PRODUCT_ID, productId); intent.putExtra(EXTRA_PRODUCT_TITLE, productTitle); return intent; } @Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_example); // restore saved state if(savedInstanceState != null) {
handleSavedInstanceState(savedInstanceState); } // handle intent extras Bundle extras = getIntent().getExtras(); if(extras != null) {
handleExtras(extras); } } @Override public void onSaveInstanceState(Bundle outState) {
// save current instance state super.onSaveInstanceState(outState); // TODO } @Override public void onRestoreInstanceState(Bundle savedInstanceState) {
// restore saved state super.onRestoreInstanceState(savedInstanceState); if(savedInstanceState != null) {
// TODO } } private void handleSavedInstanceState(Bundle savedInstanceState) {
// TODO } private void handleExtras(Bundle extras) {
// TODO }}

Fragment

When you are creating a new instance of Fragment, you can pass arguments through setArguments(Bundle) method. Data can be retrieved with getArguments() method. It would be a mistake to supply initialization data through an overloaded constructor. Fragment instance can be re-created (e.g. because of configuration changes) so you would lose data, because constructor with extra parameters is not called when re-initializing Fragment. Only empty constructor is called. Best way to solve this problem is implementing static creator method newInstance() which returns a new instance of Fragment and sets the arguments via setArguments(Bundle).

Fragment state can be saved using onSaveInstanceState(Bundle) method. It is similar to the Activity. Data can be restored in onCreate(Bundle), onCreateView(LayoutInflater, ViewGroup, Bundle), onActivityCreated(Bundle) or onViewStateRestored(Bundle) methods.

Fragment has also access to the Intent extras which were passed during creating the Activity instance. You can get this extra data by calling getActivity().getIntent().getExtras().

See example code below.

public class ExampleFragment extends Fragment{
private static final String ARGUMENT_PRODUCT_ID = "product_id"; private static final String SAVED_LIST_POSITION = "list_position"; public static ExampleFragment newInstance(String productId) {
ExampleFragment fragment = new ExampleFragment(); // arguments Bundle arguments = new Bundle(); arguments.putString(ARGUMENT_PRODUCT_ID, productId); fragment.setArguments(arguments); return fragment; } public ExampleFragment() {} @Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // handle fragment arguments Bundle arguments = getArguments(); if(arguments != null) {
handleArguments(arguments); } // restore saved state if(savedInstanceState != null) {
handleSavedInstanceState(savedInstanceState); } // handle intent extras Bundle extras = getActivity().getIntent().getExtras(); if(extras != null) {
handleExtras(extras); } } @Override public void onSaveInstanceState(Bundle outState) {
// save current instance state super.onSaveInstanceState(outState); // TODO } private void handleArguments(Bundle arguments) {
// TODO } private void handleSavedInstanceState(Bundle savedInstanceState) {
// TODO } private void handleExtras(Bundle extras) {
// TODO }}

You can find example code also on my GitHub in repo. This blogpost was inspired by on Google Plus. Gotta some questions or ideas about Bundles? Follow me on or .

posted on
2017-07-08 18:01 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/mthoutai/p/7137515.html

你可能感兴趣的文章
观察者模式
查看>>
Weka中数据挖掘与机器学习系列之基本概念(三)
查看>>
Win磁盘MBR转换为GUID
查看>>
大家在做.NET B/S项目的时候多用什么设技术啊?
查看>>
Java SE和Java EE应用的性能调优
查看>>
leetcode-Sort List
查看>>
中文词频统计
查看>>
了解node.js
查看>>
想做移动开发,先看看别人怎么做
查看>>
Eclipse相关集锦
查看>>
虚拟化架构中小型机构通用虚拟化架构
查看>>
继承条款effecitve c++ 条款41-45
查看>>
Java泛型的基本使用
查看>>
1076 Wifi密码 (15 分)
查看>>
noip模拟赛 党
查看>>
bzoj2038 [2009国家集训队]小Z的袜子(hose)
查看>>
Java反射机制及其Class类浅析
查看>>
Postman-----如何导入和导出
查看>>
移动设备显示尺寸大全 CSS3媒体查询
查看>>
图片等比例缩放及图片上下剧中
查看>>