Android Fragment Onhiddenchanged Method detailed

Source: Internet
Author: User

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, Create a shared data variable, and in other fragment just get this value to get the data, implement the share, 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;}

Use the following statement in the other fragment to get the data:

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 drop-down menu, in order to achieve the main interface different fragment switch, do a hidden and display processing, as long as the fragment class has been instantiated, To use, the direct show can be displayed, not when hide hides. If you don't understand or don't know how to use it, you can refer to my blog post: Click to open the link. After this implementation, I would like to fragment a list click on one of the list items, jump to another fragment B, of course, the value of this list item needs to be passed to fragment B, many people will think that you can not directly use the method above you can get shared data?

In fact, the difficulty is that the first time to switch to fragment B, has been instantiated, you can actually get the shared data, when the second switch to fragment B, click on the other list items, 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 is hidden), but after hiding, the code to get the data

String Instrname = ((mainactivity) getactivity ()). Getinstrname ();
Where do we put it?
My request is to get the shared data from fragment a all the way to fragment B, and show it in the list of fragment B?
The solution is actually there, before the activity in the recovery display when there is a way to monitor the completion of the display, the same for fragment also have a similar way to listen, as follows:
@Override public void Onhiddenchanged (Boolean Hidd) {if (Hidd) {String instrname = ((mainactivity) getactivity () ). Getinstrname (); System.out.println ("+instrname+" ***************++++++++++++***** "for the product name obtained from the opening order when the hedging interface resumes display);        System.out.println ("+instrname+" ***************++++++++++++***** "for the product name obtained from the opening order when the hedging interface resumes display); } else {//           = OnPause String instrname = ((mainactivity) getactivity ()) equivalent to fragment. Getinstrname ();          System.out.println ("+instrname+" ***************++++++++++++***** "for the product name obtained from the opening order when the hedging interface resumes display); }    }
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, which is also to get shared data, only need to write the code into the Ooncreateview.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Fragment Onhiddenchanged Method detailed

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.