Skip to main content

Activities in Android - ABNHive

An activity represents a single screen with a user interface. For example, a simple activity might present a set of choice buttons.

Activities in Android - ABNHive


Activities work together to form user experience in the app. For example, clicking a button on the first screen might replace that screen with another screen containing other widgets, a web page, a movie, or text information.
Activities are typically created by extending the Android class Activity. Activity class automatically takes care of creating a window for you in which you can place your user interface (UI) with the method setContentView(View). 

Methods in Activity Class.

Every Activity has Methods which we can override. 

  • onCreate(Bundle) initializes the activity. Two of the most important methods at your disposal here are setContentView(View) with a layout resource defining your UI, and findViewById(int), to retrieve the widgets in that UI that you need to interact with programmatically.
  • onPause() is where you deal with housekeeping associated with the activity being replaced in the foreground by something else (for example, the user has to deal with an incoming phone call while using your app). Because such interruptions can occur at any time, this housekeeping is quite important in ensuring that the app can resume seamlessly if the user returns to it, and to ensure that your app releases resources that it does not need when it is placed in the background by the operating system.
  • onResume() is typically invoked when an activity that has been sent to the background is brought back to the foreground.
  • onStop() - This callback is called when the activity is no longer visible.
  • onDestroy() - This callback is called before the activity is destroyed by the system.
  • onRestart() - It is called when the activity restarts after stopping it.
Lifecycle of Activity


So, Create the following activity in your android studio. And enter the code below. 

MainActivity.java

     
package com.example.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
Toast.makeText(getActivity(), "This is onCreate!",Toast.LENGTH_LONG).show();
}
@Override protected void onStart() { super.onStart(); Toast.makeText(getActivity(), "This is onStart!",Toast.LENGTH_LONG).show(); } @Override protected void onResume() { super.onResume(); Toast.makeText(getActivity(), "This is onResume!",Toast.LENGTH_LONG).show(); } @Override protected void onPause() { super.onPause(); Toast.makeText(getActivity(), "This is onPause!",Toast.LENGTH_LONG).show(); } @Override protected void onStop() { super.onStop(); Toast.makeText(getActivity(), "This is onStop!",Toast.LENGTH_LONG).show(); } @Override public void onDestroy() { super.onDestroy(); Toast.makeText(getActivity(), "This is onDestroy!",Toast.LENGTH_LONG).show(); } }

Now run and test this activity.

- Aswin Bhim Nath

Comments