Android:Bundles in Activities and Fragments

來源:互聯網
上載者:User

標籤: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    }    }


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.