Activities and Intents - IndianTechnoEra
Latest update Android YouTube

Activities and Intents

Android App Development | IndianTechnoEra

Agenda:

- Understanding activities and their lifecycle

- Creating and managing activities

- Using intents to communicate between activities

- Launching activities and handling results

This section covers activities and intents in Android, including understanding activities and their lifecycle, creating and managing activities, using intents to communicate between activities, and launching activities and handling results.


Introduction:

Activities and intents are essential components of Android app development. Activities are the building blocks of an app's user interface, while intents are used to communicate between activities and launch new activities. 

Understanding Activities and Their Lifecycle

An activity is a single screen in an Android app, which contains a user interface and handles user interactions. 

An app can have multiple activities, which can be organized into a hierarchy of parent and child activities.


The lifecycle of an activity refers to the different stages it goes through as it is created, started, resumed, paused, stopped, and destroyed. 

Understanding the activity lifecycle is important for managing app resources and ensuring that the app behaves as expected.


Creating and Managing Activities

To create an activity, a developer must create a Java class that extends the Activity class and defines the UI elements and user interactions. 

The activity class is then registered in the app's manifest file, which specifies the activity's name, label, and icon.


Once an activity is created, it can be managed using methods such as onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy(). 

These methods are called by the system at different stages of the activity lifecycle, and can be used to manage resources, handle user input, and update the UI.


For example, the following code shows how to create an activity that displays a simple text message:

```

public class MyActivity extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my);


        TextView textView = findViewById(R.id.text_view);

        textView.setText("Hello, world!");

    }

}

```

This code defines an activity called "MyActivity" that extends the AppCompatActivity class. 

The onCreate() method is overridden to set the activity's layout using the setContentView() method and to update a TextView element with the text "Hello, world!".


Using Intents to Communicate Between Activities

Intents are used to communicate between activities and launch new activities. An intent is an abstract description of an operation to be performed, such as viewing a web page or sending an email. 

Intents can be explicit, which specify the component to be invoked, or implicit, which specify the action to be performed and allow the system to choose the component.


To start a new activity using an intent, a developer can use the startActivity() method, passing in the intent as a parameter. 

The intent can also be used to pass data between activities using extras, which are key-value pairs that can be added to the intent.

For example, the following code shows how to start a new activity using an explicit intent:

```

Intent intent = new Intent(this, MySecondActivity.class);

intent.putExtra("message", "Hello, from MyActivity!");

startActivity(intent);

```

This code creates an explicit intent to start a new activity called "MySecondActivity". 

It also adds an extra to the intent with the key "message" and the value "Hello, from MyActivity!". 

The new activity can then retrieve this extra using the getIntent() method and the getExtra() method.


Launching Activities and Handling Results

When an activity is launched, it can return a result to the calling activity using the setResult() method and the finish() method. The result is returned as an intent with a result code and any additional data.


To handle the result, the calling activity can override the onActivityResult() method and retrieve the result code and data from the returned intent.

For example, the following code shows how to launch a new activity and handle the result:

```

public void launchSecondActivity() {

    Intent intent = new Intent(this, MySecondActivity.class);

    startActivityForResult(intent, REQUEST_CODE);

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_CODE) {

        if (resultCode == RESULT_OK) {

            String result = data.getStringExtra("result");

            // Do something with the result

        }

    }

}

```


This code defines a method called launchSecondActivity() that creates an intent to start a new activity called "MySecondActivity" using the startActivityForResult() method. It also specifies a request code to identify the result returned by the activity.

The calling activity then overrides the onActivityResult() method to handle the result. It checks the request code and result code to ensure that the correct result is being handled and retrieves any data from the returned intent using the getExtra() method.


Stages of Intent:

Start App         : onCreate  >> onStart  >> onResume

Home App       : onPause  >> onStop 

Recent App      : onStart  >> onResume

Close App        : onPause  >> onStop  >> onDestroy

إرسال تعليق

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.