• Tidak ada hasil yang ditemukan

Week #

N/A
N/A
Protected

Academic year: 2025

Membagikan "Week #"

Copied!
4
0
0

Teks penuh

(1)

Department of Information Technology

Mobile Application Development

Faculty of Computing and Information Technology

King Abdulaziz University

CPIT490

1

Lab#6: Intents and Applications

Objectives:

The objective of this lab is to practice invoking applications and maintaining information within an application.

Outline of this lab:

1. Understand the use of onSaveInstanceState() and onRestoreInstanceState() methods 2. Calling an application from an activity using Intents

3. Understand the use of setRetainInstance() method Activity Outcomes

At the end of this lab, the student will be able to

1. Save and retrieve information when an activity is restarted.

2. Handle the transition from an activity to an application.

3. Handle saving and retrieval of information in fragments.

Lab Tasks

Task 1: Understand the use of onSaveInstanceState() method Analyze the given Java code and find the answers for the following questions:

import java.util.Calendar;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.widget.EditText;

public class OrientationsActivity extends Activity { /** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.main);

EditText text1 = (EditText) findViewById(R.id.txtField1);

text1.setText("Initial Text");

}

@Override

public void onSaveInstanceState(Bundle outState) { Log.d("StateInfo", "onSaveInstanceState");

//---save whatever you need to persist--- outState.putString("ID", "1234567890");

// Saving a Calendar object using putSerializable() method Calendar datevalue = Calendar.getInstance();

outState.putSerializable("date", datevalue);

super.onSaveInstanceState(outState);

}

@Override

public void onRestoreInstanceState(Bundle savedInstanceState) {

(2)

2

Log.d("StateInfo", "onRestoreInstanceState");

super.onRestoreInstanceState(savedInstanceState);

//---retrieve the information persisted earlier--- String ID = savedInstanceState.getString("ID");

// Retrieve the location of the EditText

EditText text1 = (EditText) findViewById(R.id.txtField1);

// Assign the old saved value to the EditText text1.setText(ID);

// Accessing a saved Serializable object

ID = savedInstanceState.getSerializable("date").toString();

text1.setText(ID);

Calendar cal = (Calendar) savedInstanceState.getSerializable("date");

text1.setText(cal.getTime().toString());

}

@Override

public void onStart() { Log.d("StateInfo", "onStart"); super.onStart(); }

@Override

public void onResume() { Log.d("StateInfo", "onResume"); super.onResume(); }

@Override

public void onPause() { Log.d("StateInfo", "onPause"); super.onPause(); }

@Override

public void onStop() { Log.d("StateInfo", "onStop"); super.onStop(); }

@Override

public void onDestroy() { Log.d("StateInfo", "onDestroy"); super.onDestroy();}

@Override

public void onRestart() { Log.d("StateInfo", "onRestart"); super.onRestart(); } }

The layout file for this application is given below.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<EditText

android:id="@+id/txtField1"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

</LinearLayout>

1. When is the onSaveInstanceState() method called?

 Check if the method is called in between onPause() and onStop() method.

 What happens if you change the orientation of the mobile (Press Ctrl+F11 on your emulator)?

Is both onPause() and onStop() method called?

 Indicate few events that we could perform to make the onSaveInstanceState() method to be executed.

2. When is the onRestoreInstanceState() method called?

3. Press Ctrl+F11 on this program few times and see how the time changes.

Task 2: Invoking an application from an activity using Intent Refer to the code in BAAD book page numbered 85 IntentsActivity

import android.app.Activity;

import android.content.Intent;

(3)

3

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

public class IntentsActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

public void onClickWebBrowser(View view) {

Intent i = new Intent("android.intent.action.VIEW");

i.setData(Uri.parse("http://www.amazon.com"));

startActivity(i);

}

public void onClickMakeCalls(View view) {

Intent i = new Intent(android.content.Intent.ACTION_DIAL, Uri.parse("tel:+96612345678"));

startActivity(i);

}

public void onClickShowMap(View view) {

Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("geo:21.42750, 39.1270"));

startActivity(i);

}

public void onClickLaunchMyBrowser(View view) {

// net.learn2develop.MyBrowser is another program that is to be invoked // the layout used by this program is given here below

Intent i = new Intent("net.learn2develop.MyBrowser");

i.setData(Uri.parse("http://www.amazon.com"));

startActivity(i);

}

}

The layout file used by the MyBrowser program. Note the presence of WebView in this layout.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<WebView

android:id="@+id/WebView01"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

</LinearLayout>

Answer the following questions based on the given code snippet above:

1. Examine the code and find the difference between:

a. Calling an activity from another activity b. Calling an application from an activity

2. The second activity is named as net.learn2develop. baad085intentsactivity.MyBrowserActivity but when we invoke the concerned activity using Intent from the main program, we call it as

Intent i = new Intent("net.learn2develop.MyBrowser");

a.

Find out how is it possible to call MyBrowserActivity class as MyBrowser. Hint:

Refer to the manifest file.

(4)

4

b.

Learn about the various elements present in intent-filter tag present in the manifest file.

3.

Task 3: Understand the use of setRetainInstance(true) method

The OnRetainNonConfigurationInstance( ) and GetLastNonConfigurationInstance( ) methods were used to save information when there is a configuration change. The change of orientation of an application is an example of configuration change. These OnRetainNonConfigurationInstance () method is invoked between onStop() and onDestroy() methods. These two methods are currently deprecated and the use of setRetainInstance(true) is recommended. Apparently, this setRetainInstance(true) method works only with fragments. The presence of this method causes certain changes to the lifecycle of the android application.

Find the answers for the following:

1. Run the given program in your emulator and check the difference in lifecycle with and without setRetainInstance(true). Draw a flowchart that indicates the flow of the program.

2. Remove the ScrollView in the layout file and see its impact.

3. The LinearLayout used within ScrollView is oriented vertically. What if we wish to change the orientation to be horizontal?

References

http://www.intertech.com/Blog/saving-and-retrieving-android-instance-state-part-1/

http://developer.android.com/reference/android/app/Fragment.html

Referensi

Dokumen terkait

public class PencarianFragment extends Fragment { View view ;. SQLiteOpenHelper helper ; SQLiteDatabase

public class ContohJLabel extends JFrame{ public static void main(String[] args) {. ContohJLabel apl = new

CONTINUED Use the classes below to answer the next TWO questions: class ClassA { public void method1 { System.out.println"A.method1"; } } class ClassB extends ClassA { public

The code for the JPanel class for this program has been provided below: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyJPanel extends JPanel

Return type harus sama Berikut ini contoh terjadinya overriding dimana method Info pada class Child meng- override method Info pada class parent: class Parent { public void Info {

import javax.swing.*; import java.awt.event.*; public class SimpleGui1B implements ActionListener { JButton button; public static void mainString[] args { SimpleGui1B = new SimpleGui1B;

import java.io.*; import java.util.*; public class LineNumberer { public static void mainString[] args throws FileNotFoundException { Scanner console = new ScannerSystem.in;

HelloWorld1 2 MainActivity.java package com.latihan.hello1; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity