Class on Fragments & threads
Fragments fragment is a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running. Flexibility in designing the UI that single screen it is possible to accommodate more than one UI modules. Activity act as a container & host multiple fragments to show on a single screen.
Building Fragments 3 steps Designing the Fragment layout. Creating fragment class. Associating fragment to an activity.
To start with.. Design the layout of the fragment in the associated layout XML file. If 2 fragment in an XML file-define 2 layout file. Step 2 Create a class that extend fragment. This class is required to inflate the fragment layout & define the behavior of Fragment. To create a fragment, extend the Fragment class, then override key lifecycle methods to insert your app logic, similar to the way you would with an Activity class.
One difference when creating a Fragment is that you must use the onCreateView() callback to define the layout. This is the only callback you need in order to get a fragment running. Example
Just like an activity, a fragment should implement other lifecycle callbacks that manage its state as it is added or removed from the activity and as the activity transitions between its lifecycle states. For instance, when the activity's onPause() method is called, any fragments in the activity also receive a call to onPause().
Add a Fragment to an Activity using XML While fragments are reusable, modular UI components, each instance of a Fragment class must be associated with a parent FragmentActivity. You can achieve this association by defining each fragment within your activity layout XML file.
Then apply the layout to your activity:
When you add a fragment to an activity layout by defining the fragment in the layout XML file, you cannot remove the fragment at runtime. If you plan to swap your fragments in and out during user interaction, you must add the fragment to the activity when the activity first starts.
Fragments can be dynamically associated with an Activity. It is achieved by placing them in the layout file of the activity using tag. Here in the onResume() method of the Activity,we attach the required Fragments at runtime using the FragmentManager API.
FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction(); fragmentTransaction.add(R.id.namefragmentContainer,new NameFragment(),”tagNationFragment”); fragmentTransaction.commit(); Parameters of add()method-id of the placeholder for the fragment,instance of the Fragment & a tag with which the Fragment can be referred later if needed.
Life Cycle of Fragments The difference between the lifecycle of an activity and that of a fragment is Activity lifecycle is managed by Android runtime using task stack. Fragments are managed by the host activity using back stack mechanism. The back stack mechanism helps developer to manage multiple Fragment transactions in an Activity.
Design and implement long running task using Thread.Example A scenario where an Activity allows a user to start,stop and reset a counter. When the counter is running,its current value has to be continuously displayed in the TextView. If increment of counter in onClick() method,andstart the counter,subsequent tap on stop or reset result in ANR. To overcome this,need to offload the counter increment logic to a separate thread
Public void onClick(View arg0) { Switch(arg0.getId() { Case R.id.startCounterButton: keepCounting=true; New Thread(new Runnable() Public void run() { While (keepCounting) { Try { Thread.sleep(1000); } catch (InterruptedException e) { E.printStackTrace(); } Count++; displayValue.post(new Runnable() Public void run() { displayValue.setText(count +””); } }); } }).start(); Break; Case R.id.stopCounterButton: keepCounting=false; Break; Case R.id.resetButton: Count=0; displayValue.setText(count + “”); Break; }
To access a thread from a non UI thread Get hold of the main thread(Accomblish by a handler). A handler is an object which always refer to the thread that created it. The handler of the main thread allow other threads to post new tasks to a message queue maintain by the main thread. This message queue is used to line up tasks,including UI updates. Task in this queue are executed on a first come first serve basis. Once the message queue is exhausted, the main thread waits for a new task(s) in an endless loop. This is initiated by looper. When the post() method is invoked,the non-UI thread post a task to the message queue of the main thread by obtaining the handler of main thread.