activity & shared preference

16
1 Activities, Intents and Shared Preferences for Persistence

Upload: nationalmobileapps

Post on 20-Jun-2015

173 views

Category:

Education


1 download

DESCRIPTION

National Mobile Application Awareness Development & Capacity Building Program (Android Slides)

TRANSCRIPT

Page 1: Activity & Shared Preference

1

Activities, Intents and

Shared Preferences for

Persistence

Page 2: Activity & Shared Preference

SIMPLE ACTIVITY• Activity• Layout• Using Layouts from Activity• Activity to Activity Transition• Activity Lifecycle• Exercise

2

Page 3: Activity & Shared Preference

Android ActivitiesActivity

Activity provides a user generally with an interactive screen to do something like: Dialing the phone, View a map List of something for user to select or Anything you want your user to do

• An application usually consists of multiple activities.• Typically, one activity in an application is specified as the "main" activity, which is

presented to the user when launching the application for the first time. (which is specified in AndroidMenifest.xml)

The <action> element specifies that this is the "main" entry point to the application. The <category> element specifies that this activity should be listed in the system's application launcher (to allow users to launch this activity).

3

Page 4: Activity & Shared Preference

Android Activities (Contd..)

Creating Activity

We have already created Activities. But how did we create it?Let’s revisit…

1. We created subclass of Activity base class2. We implemented one callback method onCreate

1. What is creating subclass???2. What is callback methods???

Open Questions:

4

Page 5: Activity & Shared Preference

ANDROID ACTIVITIES (CONTD..)Next Step: Implementing User Interface

1. Design res/layout/yourlayout.xml2. Use Views from Activity class

Next Step: Implementing User Interface<manifest ... ><application ... > <activity android:name=".ExampleActivity" />...</application ... > ...</manifest >

Next Step: Starting Activity

Intent intent = new Intent(this, ToActivity.class);startActivity(intent);

We can start another activity by calling startActivity(), passing it an Intent that describes the activity you want to start.

5

Page 6: Activity & Shared Preference

Android Activities Exercise1. We’ll make 2 Activities2. Registration with basic features and validation:

1. Email address2. First Name, Last Name3. Date of Birth4. Password5. Confirm Password6. I agree to terms and conditions7. Buttons: Cancel, Submit

3. Login1. Email address2. Password3. Buttons: New User, Login(To be added: Remember me)

Link: http://developer.android.com/guide/appendix/faq/commontasks.html

6

Page 7: Activity & Shared Preference

Android Activities Exercise (Contd.)

1. How to add new Activities to the application2. How to navigate from one Activity to another

How to navigate from one Activity to another

Activity 1 Activity 2 Intent

Intent intent_1_to_2 = new Intent(this, Activity2.class);startActivity(intent_1_to_2);

Activity 1 Activity 2 Intent

Intent intent_2_to_1 = new Intent(this, Activity1.class);startActivity(intent_2_to_1);

7

Page 8: Activity & Shared Preference

Android Activities Exercise (Contd.)How to navigate from one Activity to another

Activity 1 Activity 2 Intent

Intent intent1to2 = new Intent(this, Activity2.class);startActivity(intent1to2);

Activity 1 Activity 2 Intent

Intent intent2to1 = new Intent(this, Activity1.class);startActivity(intent2to1);

8

Page 9: Activity & Shared Preference

Activity Lifecycle

9

Page 10: Activity & Shared Preference

Activity Lifecycle (Contd.)

10

Page 11: Activity & Shared Preference

Shared Preferences for PersistenceAndroid applications can store data in application preferences. In this tutorial, you learn how to store persistent application data with shared preferences.

1. Preferences in Android are used to keep track of application and user preferences.2. In any application, there are default preferences that can accessed through the

PreferenceManager instance and its related method getDefaultSharedPreferences(Context)

3. With the SharedPreference instance one can retrieve the int value of the any preference with the getInt(String key, int defVal).

4. In our case, we can modify the SharedPreference instance in our case using the edit() and use the putInt(String key, int newVal)

11

Page 12: Activity & Shared Preference

Shared Preferences for Persistence

We see an example of this:1. We create a project:2. We also create a layout

12

Page 13: Activity & Shared Preference

Shared Preferences for PersistenceHere is our Activity

13

Page 14: Activity & Shared Preference

Shared Preferences for PersistenceWe read and write now:

14

Page 15: Activity & Shared Preference

Shared Preferences for Persistence

Now we run the app:1. For testing we set a value2. Exit application3. Launch the app again4. Retrieve the value

15

Page 16: Activity & Shared Preference

Thank You.

16