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