Department of Information Technology
Mobile Application DevelopmentFaculty of Computing and Information Technology
King Abdulaziz University
CPIT4901
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
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
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.