[Android] (study note 5) using fragment with parameters

Source: Internet
Author: User

Before you get to the point, let's briefly describe a scenario:

There is now a activity1 and a activity2, where Activity2 is a fragment managed activity. Activity1 uses intent to start activity2,intent with parameters. These parameters need to be displayed as displays on the Activity2 managed fragment.

There are two methods, the first is relatively simple, when Activity2 created fragment, in Fragment's OnCreate method through Getactivity () to get the Activity2 object to host it, and then by calling Getintent ( method to get the intent object passed from Activity1 to Activity2, and then getextraxxxxxx. This method is simple, but there is a problem, that is, fragment know the details of their hosting activity, this fragment can only be used with activity2 binding.

If you want fragment to remain independent relative to Activity2, you can use the fragment with parameters, which is method two. For fragment to have parameters, you can use the Fragment.setarguments () method. This method requires a bundle object. The bundles in Android contain key-value pairs, which are much like intent. However, it is important to note that the method must be called before fragment is created and added to the activity.

Back to the scene just mentioned, Activity2 got a intent passed from Activity1, need to create fragment. Add a static method to fragment to generate the Fragment object (the method name is Newinstance () and receive the parameters that may be required). In this static method, the action of fragment from instantiation to additional parameters is completed, and the instantiated fragment object is returned to the managed Activity2. In this way, activity2 to create fragment, it is important to pass the data you have acquired to fragment's instantiation method newinstance. Next, fragment can access the data obtained from its managed activity2 directly in its life cycle functions.

It is important to note that interactive activity and argument do not need and cannot maintain universal independence at the same time. Hosting activity understands the internal details of fragment, which is normal, and conversely, fragment does not have to know the details of its hosting activity.

[Android] (study note 5) using fragment with parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.