Android fragment-Add a fragment without a UI

Source: Internet
Author: User

The above example shows how to add fragment to the activity as part of the UI. However, you can also use fragment to provide only one background behavior without any additional UI display.

To add a fragment without a UI, you must use the add (fragment, string) (to provide a unique string "tag" for fragment, rather than the view ID) method to add fragment in the activity. However, because the added fragment is not associated with the view in the activity layout, it does not accept calls to the oncreateview () method,

Therefore, you do not need to implement this method.

It cannot be said that the fragment that provides the string "tag" is a non-uifragment, because you can also provide the string "tag" for the fragment with the UI, but if the fragment does not have the UI, you can only use the string method to identify it. If you want to obtain the fragment from the activity, use the findfragmentbytag () method.

 

Fragmentretaininstance. Java demonstrates an activity that uses fragment as the background worker without a UI.

/*
* Copyright (c) 2010 the android open source project
*
* Licensed under the Apache license, version 2.0 (the "License ");
* You may not use this file before t in compliance with the license.
* You may obtain a copy of the license
*
* Http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* Distributed under the license is distributed on an "as is" basis,
* Without warranties or conditions of any kind, either express or implied.
* See the license for the specific language governing permissions and
* Limitations under the license.
*/

Package com. example. Android. APIs. app;

Import com. example. Android. APIs. R;

Import Android. App. activity;
Import Android. App. Fragment;
Import Android. App. fragmentmanager;
Import Android. OS. Bundle;
Import Android. View. layoutinflater;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. progressbar;

/**
* This example shows how you can use a fragment to easily propagate state
* (Such as threads) Wait SS activity instances when an activity needs to be
* Restarted due to, for example, a configuration change. This is a lot
* Easier than using the raw activity. onretainnonconfiguratininstance () API.
*/
Public class fragmentretaininstance extends activity {
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );

// First time init, create the UI.
If (savedinstancestate = NULL ){
Getfragmentmanager (). begintransaction (). Add (Android. R. Id. content,
New uifragment (). Commit ();
}
}

/**
* This is a fragment showing UI that will be updated from work done
* In the retained fragment.
*/
Public static class uifragment extends fragment {
Retainedfragment mworkfragment;

@ Override
Public View oncreateview (layoutinflater Inflater, viewgroup container,
Bundle savedinstancestate ){
View v = Inflater. Inflate (R. layout. fragment_retain_instance, container, false );

// Watch for button clicks.
Button button = (button) v. findviewbyid (R. Id. Restart );
Button. setonclicklistener (New onclicklistener (){
Public void onclick (view v ){
Mworkfragment. Restart ();
}
});

Return V;
}

@ Override
Public void onactivitycreated (bundle savedinstancestate ){
Super. onactivitycreated (savedinstancestate );

Fragmentmanager fm = getfragmentmanager ();

// Check to see if we have retained the worker fragment.
Mworkfragment = (retainedfragment) fm. findfragmentbytag ("work ");

// If not retained (or first time running), we need to create it.
If (mworkfragment = NULL ){
Mworkfragment = new retainedfragment ();
// Tell it who it is working.
Mworkfragment. settargetfragment (this, 0 );
FM. begintransaction (). Add (mworkfragment, "work"). Commit ();
}
}

}

/**
* This is the fragment implementation that will be retained into SS
* Activity instances. It represents some ongoing work, here a thread
* We have that sits around incrementing a progress indicator.
*/
Public static class retainedfragment extends fragment {
Progressbar mprogressbar;
Int mposition;
Boolean mready = false;
Boolean mquiting = false;

/**
* This is the thread that will do our work. It sits in a loop running
* The progress up until it has reached the top, then stops and waits.
*/
Final thread mthread = new thread (){
@ Override
Public void run (){
// We'll figure the real value out later.
Int max = 10000;

// This thread runs almost forever.
While (true ){

// Update our shared state with the UI.
Synchronized (this ){
// Our thread is stopped if the UI is not ready
// Or it has completed its work.
While (! Mready | mposition> = max ){
If (mquiting ){
Return;
}
Try {
Wait ();
} Catch (interruptedexception e ){
}
}

// Now update the progress. Note it is important that
// We touch the progress bar with the lock held, so it
// Doesn' t disappear on us.
Mposition ++;
Max = mprogressbar. getmax ();
Mprogressbar. setprogress (mposition );
}

// Normally we wocould be doing some work, but put a kludge
// Here to pretend like we are.
Synchronized (this ){
Try {
Wait (50 );
} Catch (interruptedexception e ){
}
}
}
}
};

/**
* Fragment initialization. We way we want to be retained and
* Start our thread.
*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );

// Tell the framework to try to keep this fragment around
// During a configuration change.
Setretaininstance (true );

// Start up the worker thread.
Mthread. Start ();
}

/**
* This is called when the fragment's activity is ready to go, after
* Its content view has been installed; it is called both after
* The initial fragment creation and after the fragment is re-attached
* To a new activity.
*/
@ Override
Public void onactivitycreated (bundle savedinstancestate ){
Super. onactivitycreated (savedinstancestate );

// Retrieve the progress bar from the target's view hierarchy.
Mprogressbar = (progressbar) gettargetfragment (). getview (). findviewbyid (
R. Id. progress_horizontal );

// We are ready for our thread to go.
Synchronized (mthread ){
Mready = true;
Mthread. Y ();
}
}

/**
* This is called when the fragment is going away. It is not called
* When the fragment is being propagated between activity instances.
*/
@ Override
Public void ondestroy (){
// Make the thread go away.
Synchronized (mthread ){
Mready = false;
Mquiting = true;
Mthread. Y ();
}

Super. ondestroy ();
}

/**
* This is called right before the fragment is detached from its
* Current Activity instance.
*/
@ Override
Public void ondetach (){
// This fragment is being detached from its activity. We need
// To make sure its thread is not going to touch any activity
// State after returning from this function.
Synchronized (mthread ){
Mprogressbar = NULL;
Mready = false;
Mthread. Y ();
}

Super. ondetach ();
}

/**
* API for our UI to restart the progress thread.
*/
Public void restart (){
Synchronized (mthread ){
Mposition = 0;
Mthread. Y ();
}
}
}
}

 

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.