Android simply changed the activity to fragment

Source: Internet
Author: User

Developing Android sometimes comes across the problem of transforming activity into fragment. Originally fragment is the new Android version of the stuff, but since Google is so gracious to provide a compatible package. Then there will always be such a need, fortunately, the transformation of activity to fragment is not complicated, because it is a similar thing. Recently encountered this problem, so to summarize the experience.

The biggest change from activity to fragment is that within the OnCreate () function inside the fragment, there is no way to use Findviewbyid, Setcontentview these functions. Because fragment itself does not provide these methods. So we can only use Oncreateview and onviewcreated to achieve the control of the display.

@Override

public view Oncreateview (layoutinflater inflater, viewgroup container< Span class= "Crayon-sy" >, bundle Savedinstancestate { return inflater. Inflate(R. Layout. About_detail, container, false); }@Override public void onviewcreated(view view, Bundle savedinstancestate ) { Super. onviewcreated(view, savedinstancestate);   version = (TextView view. Findviewbyid (r. Id. Version loginfo = (TextView) view. Findviewbyid(R. ID. Loginfo); } and some fragment do not provide a function that belongs to activity we can get through getactivity () as the activity of the parent to invoke, because fragment is only provided as fragments to the activity to use, So the implementation of many functions is left in the activity. However, this can be done very quickly by using the error-checking feature of Eclipse. This change is generally not very complex, as long as the fragment to be done to the activity as a parent to deal with it. For example, a intent like this can be done:

Intent i = new Intent ();
I.setclass (Getactivity (). Getapplicationcontext (), newactivity.class);
StartActivity (i);
Getactivity (). Finish ();

http://blog.ch-wind.com/android%E7%AE%80%E5%8D%95%E7%9A%84%E5%B0%86activity%E6%94%B9%E4%B8%BAfragment/

Android simply changed the activity to fragment

Related Article

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.