Android fragment Onhiddenchanged Method specific explanation

Source: Internet
Author: User

The project. In order to achieve the difference between the fragment, the first thought is the use of bundles or shareprefercnce and other shared data mechanism, when not meet the project requirements, a bit too complex and redundant, the use of a senior online advice. In the main activity, the creation of a shared data variable, in other fragment only to obtain this value can be obtained data, to achieve sharing. The code is as follows:

The main activity defines a shared data variable:   private String Instrname;public string Getinstrname () {return instrname;} public void Setinstrname (String instrname) {this.instrname = Instrname;}

This data can be obtained in other fragment using the following statements, for example:

String Instrname = ((mainactivity) getactivity ()). Getinstrname ();

But the point is not to get the data, I encountered the difficulty is this, the previous blog has mentioned that I used Popupwindow as the left side drop-down menu, in order to achieve the main interface different fragment switch, do a hidden and display processing. Just have to instantiate the fragment class, to use the time, the direct show display can, when not hidden hide.

Do not understand or do not know how to use the can participate in my blog: Click to open the link. Once this is done, I'm going to fragment a list of items in a list. Jump to another fragment B, of course, the value of this list item needs to be passed to fragment B. A lot of people will feel that it is not directly able to use the above method can you get shared data?

In fact, the difficulty is to switch to fragment B for the first time. When it is already instantiated. You can actually get the shared data, and when you switch to fragment B for the second time, the other list items are clicked. This time I found that this fragment B has been hidden by me. And it is this: (the initialized fragment is no longer initialized, only hide hides), but after hiding, the code that gets the data

String Instrname = ((mainactivity) getactivity ()). Getinstrname ();
Where should you put it to run?
My request is to get the shared data immediately from fragment A to fragment B, and show up in the list of fragment B now?
The solution is actually some, once activity in the recovery display when there is a way to monitor the display is complete, the same for fragment also have a similar method can be monitored. For example, the following:
@Override public void Onhiddenchanged (Boolean Hidd) {if (Hidd) {String instrname = ((mainactivity) getactivity () ). Getinstrname (); System.out.println ("+instrname+" ***************++++++++++++***** "for the name of the product to be obtained from the opening order when the hedging interface resumes its display again);        System.out.println ("+instrname+" ***************++++++++++++***** "for the name of the product to be obtained from the opening order when the hedging interface resumes its display again); } else {//           = OnPause String instrname = ((mainactivity) getactivity ()) equivalent to fragment. Getinstrname ();          System.out.println ("When the hedging interface again resumes display.")        Obtain the product name from the opening order "+instrname+" ***************++++++++++++***** "); }    }
This allows the shared data to be obtained when the fragment is hidden or displayed.Note that the first time is to need a new Frament object. This is also the time to get shared data. Just need to write the code into the Ooncreateview.

Android fragment Onhiddenchanged Method specific explanation

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.