Mobile App development Chapter three
Mobile App development Chapter three
• For your app to be able to use activities, you must declare the activities, and
certain of their attributes, in the manifest.
• The only required attribute for this element is android:name, which specifies the
class name of the activity. You can also add attributes that define activity
characteristics such as label, icon, or UI theme.
Understanding the Android Activity Life Cycle
• Android apps do not have a “main” method; you need to understand how they get
started and how they stop or get stopped.
• The class android.app.Activity provides a number of well-defined life-cycle
methods
that are called when an application is started, suspended, restarted, and so on, as
well as a method you can call to mark an Activity as finished.
onCreate()
• The onCreate() callback is compulsory in all Android applications.
• It is the first method called when we launch an activity from the home screen or
intent.
• In other words, it is a default callback that is automatically created when you
create a new activity.
• protected void onCreate (Bundle savedInstanceState)
• Called when the activity is starting. This is where most initialization should go:
calling setContentView(int) to inflate the activity's UI, using findViewById(int) to
programmatically interact with widgets in the UI to retrieve cursors for data being
displayed, etc.
▪ You can call finish() from within this function, in which case
onDestroy() will be immediately called
after onCreate(Bundle) without any of the rest of the activity lifecycle
(onStart(), onResume(), onPause(), etc) executing.
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "onCreate MainActivity",
Toast.LENGTH_SHORT).show();
Log.d(TAG, "onCreate MainActivity");
}
onStart()
• Called after onCreate(Bundle) — or after onRestart() when the activity had been
stopped, but is now again being displayed to the user.
• protected void onStart ()
• It will usually be followed by onResume(). This is a good place to begin drawing
visual elements, running animations, etc.
• You can call finish() from within this function, in which case onStop() will be
immediately called after onStart() without the lifecycle transitions in-between
(onResume(), onPause(), etc) executing.
Here is how onStart() is implemented.
@Override
protected void onStart() {
Toast.makeText(this, "onStart MainActivity",
Toast.LENGTH_SHORT).show();
Log.d(TAG, "onStart MainActivity");
super.onStart();
}
onResume()
• Once onStart() is called, onResume() is immediately invoked.
• Every component associated with this activity is brought to the foreground state.
The activity is now considered interactive.
• protected void onResume ()
• Called after onRestoreInstanceState(Bundle), onRestart(), or onPause().
• This is usually a hint for your activity to start interacting with the user, which is a
good indicator that the activity became active and ready to receive input.
onPause()
• Since the activity’s states still exist, the onRestart() method can be
called when the user restarts the activity. This means the activity will
go back to the main screen and the user can resume interacting with
its components.
• when the onRestart() method is executed, the activity will resume by
executing the onStart() then onResume(). Since the onCreate()
function is only called once in an activity’s life-cycle.
onDestroy()
• This is the final callback that the activity will receive when it is stopped.
• The method is called when there is a change in the configuration states
such as screen rotation or language settings.
• The Android system will destroy the activity, then recreate it with the
set configurations.
Android Intent
• Android Intent is the message that is passed between components
such as activities, content providers, broadcast receivers, services etc.
• It is generally used with startActivity() method to invoke activity,
broadcast receivers etc.
Android intents are mainly used to:
• Start the service
• Launch an activity
• Display a web page
• Display a list of contacts
• Broadcast a message
• Dial a phone call etc.
Types of Android Intents
• There are two types of intents in android: implicit and explicit.
1) Implicit Intent
Implicit Intent doesn't specifiy the component. In such case, intent
provides information of available components provided by the system
that is to be invoked.
For example, you may write the following code to view the webpage.
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.javatpoint.com"));
startActivity(intent);
2) Explicit Intent
• Explicit Intent specifies the component. In such case, intent provides the
external class to be invoked.
Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
startActivity(i);
• Android Explicit intent specifies the component to be invoked from activity.
In other words, we can call another activity in android by explicit intent.
• We can also pass the information from one activity to another using explicit
intent.
• Here, we are going to see an example to call one activity from another and
vice-versa.
Android calling one activity from another activity
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("Value1", "Android By Javatpoint");
i.putExtra("Value2", "Simple Tutorial");
startActivity(i);
The second activity access the data using
Bundle extras = getIntent().getExtras();
String value1 = extras.getString("Value1");
String value2 = extras.getString("Value2");
Bundles are used with intent and values are sent and retrieved in
the same fashion, as it is done in the case of Intent. It depends on
the user what type of values the user wants to pass, but bundles can
hold all types of values (int, String, boolean, char) and pass them to
the new activity
• Android uses ACTION_SEND event of android.content.Intent class to
send data from one activity to another and from current activity to
outside the application. Intent class needs to specify the data and its
type which is to be share.
Android Fragments
• Android Fragment is the part of activity, it is also known as sub-
activity.
• There can be more than one fragment in an activity. Fragments
represent multiple screen inside one activity.
• Android fragment lifecycle is affected by activity lifecycle because
fragments are included in activity.
Android service
• Android service is a component that is used to perform operations on
the background such as playing music, handle network transactions,
interacting content providers etc. It doesn't has any UI (user
interface).
• The service runs in the background indefinitely even if application is
destroyed.
• Moreover, service can be bounded by a component to perform
interactivity and inter process communication (IPC).
• The android.app.Service is subclass of ContextWrapper class.
• Life Cycle of Android Service
• There can be two forms of a service. The lifecycle of service can follow
two different paths: started or bound.
1.Started
2.Bound
• 1) Started Service
• A service is started when component (like activity)
calls startService() method, now it runs in the background
indefinitely. It is stopped by stopService() method. The service can
stop itself by calling the stopSelf() method.
• 2) Bound Service
• A service is bound when another component (e.g. client)
calls bindService() method. The client can unbind the service by
calling the unbindService() method.
• The service cannot be stopped until all clients unbind the service.
Broadcast Receivers
• Broadcast Receivers simply respond to broadcast messages from
other applications or from the system itself. These messages are
sometime called events or Intents.
Creating the Broadcast Receiver
• A broadcast receiver is implemented as a subclass
of BroadcastReceiver class and overriding the onReceive()
method where each message is received as a Intent object
parameter.
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.",
Toast.LENGTH_LONG).show();
}
}
Registering Broadcast Receiver