Android - API - Activity

Each Android application has one or more Activities.
Each Activity has a lifecycle.
Each screen we want to use as an interactive component is an Activity.
So this application component must extends the Activity class.
Interactive component is a component that the user uses to do something, for example, send an email, take a photo, etc.

1. Lifecycle of an Activity

There is below their fundamental methods:

public class ExampleActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // The activity is being created.
    }
    @Override
    protected void onStart() {
        super.onStart();
        // The activity is about to become visible.
    }
    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
    }
    @Override
    protected void onPause() {
        super.onPause();
        // Another activity is taking focus (this activity is about to be "paused").
    }
    @Override
    protected void onStop() {
        super.onStop();
        // The activity is no longer visible (it is now "stopped")
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
    }
}


So an activity may be used in interaction with several activities.
When an activity starts another one, the first is always in the stack (the back stack).
The second one may send data to the first with, for example, a return value and after the end of the second one, the first activity receives this data to use it.

2. Fragment

A Fragment is a portion of an Activity.
We can see this portion on the screen or not and they can be reuse on another Activity.
Fragments follow the lifecycle of their Activity but they have their own lifecycle.
They can be added in a ViewGroup itself inside an Activity View.
Fragments were introduced in Android 3.0 Honeycomb, especially for tablets. Main goal is to avoid several activities when less is possible.

As the Activity, the Fragment has to implement at least three methods:

onCreate();
onCreateView();
onPause();

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.