Learning Android by Marko Gargenta & Masumi Nakamura
Author:Marko Gargenta & Masumi Nakamura
Language: eng
Format: epub
Tags: COMPUTERS / Programming Languages / Java
ISBN: 9781449336240
Publisher: O’Reilly Media
Published: 2014-01-09T05:00:00+00:00
This is why, in our example, we inflated the layout in onCreateView() so that when the activity is created, the view is created accordingly.
Figure 8-1. Activity and fragment life cycle
Dynamically Adding Fragments
In Example 8-5, we have added our status fragment to the activity by creating an XML layout file that our activity inflated, at which time the fragment was also loaded and created. Because initialization of the fragment happens in an XML file, this is called static initialization. At a closer look, you may notice that most of the XML file itself is almost just noise—it’s there just to define that we need to load a fragment.
As you know from before, XML ultimately always becomes Java, so everything that can be done statically can also be done dynamically. Sometimes one approach is cleaner than others. Just for comparison purposes, let’s refactor our code to attach status fragment to status activity dynamically.
Our status activity can now be seen in Example 8-6, which shows how to load a fragment dynamically (in other words, without the need for an XML file).
Example 8-6. StatusActivity, refactored
package com.marakana.android.yamba; import android.os.Bundle; public class StatusActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Check if this activity was created before if (savedInstanceState == null) { // // Create a fragment StatusFragment fragment = new StatusFragment(); // getFragmentManager() .beginTransaction() .add(android.R.id.content, fragment, fragment.getClass().getSimpleName()) .commit(); // } } }
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6556)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6445)
Kotlin in Action by Dmitry Jemerov(5092)
Odoo 15 Development Essentials - Fifth Edition by Daniel Reis & Greg Mader(3513)
Odoo 15 Development Essentials by Daniel Reis(2837)
React Native - Building Mobile Apps with JavaScript by Novick Vladimir(2551)
Learning Angular - Second Edition by Christoffer Noring(2381)
Pride and Prejudice by Jane Austen(2370)
Mobile Forensics Cookbook by Igor Mikhaylov(2040)
Computers For Seniors For Dummies by Nancy C. Muir(2023)
Bulletproof Android: Practical Advice for Building Secure Apps (Developer's Library) by Godfrey Nolan(1893)
Android Development with Kotlin by Marcin Moskala & Igor Wojda(1817)
Building Android UIs with Custom Views by Raimon Ràfols Montané(1810)
1936941139 (N) by Bob Rosenthal(1757)
Building Progressive Web Apps: Bringing the Power of Native to the Browser by Ater Tal(1748)
Hands-On Internet of Things with MQTT by Tim Pulver(1731)
Android App Development by Franceschi Hervé J.;(1729)
Ember.js in Action by Joachim Haagen Skeie(1711)
Hands-On Design Patterns with React Native by Mateusz Grzesiukiewicz(1670)
