Questions about the Onattach () method in fragment sometimes fail to load properly

Source: Internet
Author: User

There are a few minor problems with the development of a compatible tablet app in Android studio using fragment. Since the development was designed to be compatible with mobile phones and tablets, A and B two simulators were used, one for test phones and one for test tablets. There was no problem with emulator a running the app, and Emulator B ran the app but showed an exception.

Simulator a works as follows:

The emulator B app effect runs as follows, with the same fragment on the left title page and the home page, when the app runs without the data being loaded.

It is suspected that when the app starts, some methods do not execute properly, causing some application data to not load. Because the initialization operation is done in Onattach (). Looking at the code, the editor prompts you to find that Onattach () in fragment has two methods to overwrite (activity activity), one is Onattach (context context), and the former is obsolete. I am using the Onattach (context context). Add the output information to the code.

 Public void Onattach (Context context) {    superOnattach (context);    Initnews ();     New Newsadapter (context,r.layout.news_item,newslist);    " Onattach ");}

The comparison found that when emulator a runs the application, Onattach (context context) is executed normally, and this method is not executed once it is deployed to emulator B. After replacing the Onattach (context context) with Onattach (activity activity), the deployment to emulator A, Onattach () executes normally and the problem is resolved.

Questions about the Onattach () method in fragment sometimes fail to load properly

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.