Tuesday, 24 June 2014

Shared Preferences in Android



The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

To write values:
1. Call edit() to get a SharedPreferences.Editor.
2. Add values with methods such as putBoolean() and putString().
3. Commit the new values with commit()

String name = editName.getText().toString();
SharedPreferences sp = getSharedPreferences("prefs", 0); 
SharedPreferences.Editor editor = sp.edit();
editor.putString("enteredname", name); 
editor.commit();


To read values, use SharedPreferences methods such as getBoolean() and getString().

SharedPreferences sp = getSharedPreferences("prefs", 0); 
String name = sp.getString("enteredname", "no name found");

Android Activity Life Cycle


       An Activity is usually a single screen that the users sees on the device at a one time. Any      application that has been developed by anyone it typically contains multiple activities. 


* Most management of the life cycle is done automatically by the system via the activity Stack .

* Activity manager is responsible for creating , destroying and managing activities.

In Activity Life Cycle we are having following methods: 

1. onCreate() 2. onStart() 3. onRestart() 4. onResume() 5. onPause() 6. onStop() 7. onDestroy()


1.The activity first time loads the methods are called as below: 
   onCreate() onStart() onResume()

2.When try to click back button or finish the activity the following methods are called: 

   onPause() onStop() onDestroy()

3.When you click on home button the activity goes to background and below methods are called: 

   onPause() onStop()

4.After hitting home button again if your opening the previous activity which is in background the following methods are called:

   onRestart() onStart() onResume().





Firsttime Launch:





















com.example.activitylifecycle E/onCreate ------:   MainActivity: onCreate()
com.example.activitylifecycle E/onStart ------:      MainActivity: onStart()
com.example.activitylifecycle E/onResume ------: MainActivity: onResume()



Start AnotherActivity:

com.example.activitylifecycle E/onPause ------: MainActivity: onPause()
com.example.activitylifecycle E/onCreate ------:    AnotherActivity: onCreate()
com.example.activitylifecycle E/onStart ------:       AnotherActivity: onStart()
com.example.activitylifecycle E/onResume ------:  AnotherActivity: onResume()
com.example.activitylifecycle E/onStop ------:    MainActivity: onStop()

Press BackButton:

com.example.activitylifecycle E/onPause ------AnotherActivity: onPause()
com.example.activitylifecycle E/onRestart ------: MainActivity: onRestart()
com.example.activitylifecycle E/onStart ------:     MainActivity: onStart()
com.example.activitylifecycle E/onResume ------:MainActivity: onResume()
com.example.activitylifecycle E/onStop ------: AnotherActivity: onStop()
com.example.activitylifecycle E/onDestroy ----AnotherActivity: onDestroy()

Complete back mainactivity:

activitylifecycle E/onPause ------:                         MainActivity: onPause()
com.example.activitylifecycle E/onStop ------:     MainActivity: onStop()
com.example.activitylifecycle E/onDestroy ------:MainActivity: onDestroy()


Home  Button:
E/onPause ------: MainActivity: onPause()

com.example.activitylifecycle E/onStop ------: MainActivity: onStop()



Monday, 23 June 2014

Building Blocks of Android



 Application Components

         Main  Building blocks are components that you use as an application developer to build Android apps.

         Application Components are the essential building blocks of an Android application. Each component is a different point through which the system can enter your application. Not all components are actually entry points for the user and some depend on each other, but each one exists as its own entity and plays a specific role-each one is a unique building block that helps define your application’s overall behavior.

There are four different types of application components.
·      Activities
·      Services
·      Content Providers
·      Broadcast Receivers

Activities:
                 An activity represents a single screen with a user interface. For example, an message application might have one activity that shows a list of new messages, and another activity to create a new message, and another activity to reading messages.
Although all activities work together to form a cohesive user experience in the messages application, each one independent of the others.

Services:
             Services run in the background and don’t have any user interface components. They can perform the same actions as Activities without any user interface. Services are useful for actions . For example , a service might play music in the background while user is in a different application.

Services have a much simpler lifecycle than activities. You start a service , or stop it.
Also, the service lifecycle is more or less controlled by the developer, and not so much by the system. So, we as developers have to be mindful to run our services that they don’t unnecessarily consume shared resources, such as CPU and battery.

Content Providers:
              Content Providers are interfaces for sharing data between applications. Android by default runs each application in its own sandbox so that all data that belongs to an application is totally isolated from other application on the system.

You can store the data in the file system , an SQLite database, on the web, or any other persistent storage location your application can access. Through the Content Provider , other applications can query or even modify the data (if the content provider allows it) .

Broadcast Receivers:
                        A broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcast originate from the system.

The system itself broadcasts events all the time. For example, when an SMS arrives , or call comes in, or battery runs low, or system gets booted, all those events are broadcasted and any number of receivers could be triggered by them.

You can also send your own broadcasts from one part of your application to another, or a totally different application.

Broadcast receivers themselves do not have any visual representation nor are they actively running in memory . But when triggered, they get to execute some code , such as start an activity, a service , or something else.

Friday, 20 June 2014

Android and Versions

 Android 

Android is an open source and Linux-based Operating System for mobile devices such as smartphones and tablet computers. Android was developed by the Open Handset Alliance, led by Google, and other companies.
  • Initially, Andy Rubin founded Android Incorporation in Palo Alto, California, United States in October, 2003.
  • In 17th August 2005, Google acquired android Incorporation. Since then, it is in the subsidiary of Google Incorporation.
  • The key employees of Android Incorporation are Andy RubinRich MinerChris White and Nick Sears.

Android Versions, Codename and API

Let's see the android versions codename and API provided by Google.
VersionCodenameAPI
1.5Cupcake3
1.6Donut4
2.1Eclair7
2.2Froyo8
2.3Gingerbread9 and 10
3.1 and 3.3Honeycomb12 and 13
4.0Ice Cream Sandwitch15
4.1, 4.2 and 4.3Jelly Bean16, 17 and 18
4.4KitKat19

Download Process :

1)      Open http://developer.android.com/sdk/index.html  or Type in Google “adt bundle windows” and Select Android SDK | Android Developers.

2)      Click Download the SDK (ADT Bundle for Windows ).

3)      Check Conditions.

4)      Select Which bit of Software we need.( either 32-bit or 64-bit ).

5)      Click “Download the SDK ADT Bundle for Windows.
(Software will be downloaded in zip format so we need unzipping software)

     
res/layout-port/                    XML file defining UI layout (vertical mode).
/res/layout-land/                  XML file defining UI layout (horizontal mode).