码迷,mamicode.com
首页 > 移动开发 > 详细

Android:Bundles in Activities and Fragments

时间:2014-07-21 10:11:31      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:android   style   java   color   os   io   

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_my);

        // 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 withgetArguments() 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 callinggetActivity().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
    }
    
}


 

Android:Bundles in Activities and Fragments,布布扣,bubuko.com

Android:Bundles in Activities and Fragments

标签:android   style   java   color   os   io   

原文地址:http://my.oschina.net/xsjayz/blog/292304

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!