Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

Introduction to Activities in Android

In Android, an Activity represents a single screen with a user interface. It's a fundamental component of any Android application. Each activity is independent and can start other activities, and it has its own lifecycle which defines how it is created, destroyed, and transitions between foreground and background states.

Key Features of Activities:

  • Lifecycle Management: Every activity in Android has a lifecycle that defines various states it can exist in, such as starting, running, paused, and stopped. Android provides callback methods, allowing you to perform certain actions when the activity transitions between these states.

  • UI Hosting: Activities typically contain the UI components with which users interact.

  • Intent Mechanism: Activities can start other activities using intents. Intents can be explicit (targeting a specific activity) or implicit (targeting any suitable activity based on action and data).

  • Back Stack Management: When an activity starts another, the new activity is pushed onto the back stack and takes user focus. The previous activity remains in the stack. The back stack follows the "Last In, First Out" principle, ensuring intuitive navigation for users.

Activity Lifecycle:

The lifecycle of an activity is defined by a series of callback methods:

  • onCreate(): Called when the activity is first created. This is where you'll usually perform one-time initializations, like setting up your layout.

  • onStart(): Called just before the activity becomes visible to the user.

  • onResume(): Called just before the activity starts interacting with the user. At this point, the activity is in the foreground and is receiving user input.

  • onPause(): The counterpart to onResume(). This is called when the system is about to resume a previous activity or when the activity is still partially visible, but the user is probably navigating away from it.

  • onStop(): Called when the activity is no longer visible to the user.

  • onDestroy(): Called before the activity is destroyed, which can be either because the activity is finishing (due to the user action or finish() being called) or because the system is temporarily destroying it to save space.

  • onRestart(): Called when a stopped activity is about to be started again.

Basic Example:

  • Define the Activity:

In the AndroidManifest.xml:

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Kotlin code (MainActivity.kt):

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    // Other lifecycle methods and logic can be added as needed.
}

Intents:

To start another activity, you'd use an Intent:

val intent = Intent(this, AnotherActivity::class.java)
startActivity(intent)

Conclusion:

The Activity class is a crucial part of Android applications, providing the primary means by which users interact with apps. Understanding the lifecycle of an activity and properly managing transitions and states is fundamental for building robust Android applications.

  1. Creating and launching activities in Android:

    To create an activity, define a subclass of the Activity class and override the necessary lifecycle methods. To launch an activity, use an Intent. Here's a basic example:

    public class MyActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);
    
            // Activity initialization code
        }
    }
    
  2. Working with intents to start activities in Android:

    Intents are used to start activities in Android. You can use explicit intents (specifying the target activity) or implicit intents (specifying the action, and the system determines the appropriate activity).

    // Explicit Intent
    Intent explicitIntent = new Intent(this, TargetActivity.class);
    startActivity(explicitIntent);
    
    // Implicit Intent
    Intent implicitIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    startActivity(implicitIntent);
    
  3. Passing data between activities in Android:

    Data can be passed between activities using the Intent extras. Here's an example of passing data from one activity to another:

    Intent intent = new Intent(this, TargetActivity.class);
    intent.putExtra("key", "Hello from the first activity!");
    startActivity(intent);
    

    In the target activity:

    Intent intent = getIntent();
    String message = intent.getStringExtra("key");
    
  4. Managing multiple activities in Android app:

    Android apps often involve multiple activities. Use the startActivity() method to launch new activities and the finish() method to close the current activity.

    // Start a new activity
    Intent intent = new Intent(this, NewActivity.class);
    startActivity(intent);
    
    // Finish the current activity
    finish();
    
  5. ActivityForResult in Android development:

    startActivityForResult() is used when you expect a result from the launched activity. The result is handled in the onActivityResult() method.

    // Start activity for result
    Intent intent = new Intent(this, ResultActivity.class);
    startActivityForResult(intent, REQUEST_CODE);
    
    // Handle result
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                // Handle the result
            }
        }
    }
    
  6. Handling orientation changes in Android activities:

    During orientation changes, the activity is destroyed and recreated. You can handle this by saving and restoring the activity's state using the onSaveInstanceState() and onRestoreInstanceState() methods.

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("key", value);
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        String value = savedInstanceState.getString("key");
    }