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

How to send data from one activity to the second activity in Android

In Android, you can send data from one Activity to another using Intents. Here's how you can achieve this:

1. Sending Data

In your source Activity, you'll create an Intent to start the destination Activity and put extra data into that Intent.

val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("KEY_STRING", "Hello from MainActivity!")
intent.putExtra("KEY_INT", 123)
startActivity(intent)

In this example:

  • "KEY_STRING" and "KEY_INT" are string keys that you'll use to retrieve the data in the second activity.
  • "Hello from MainActivity!" and 123 are the actual data you're sending.

2. Receiving Data

In your destination Activity (SecondActivity in this case), you'll retrieve the data from the Intent.

val bundle: Bundle? = intent.extras
val stringValue: String? = bundle?.getString("KEY_STRING")
val intValue: Int? = bundle?.getInt("KEY_INT")

// Now you can use stringValue and intValue in your activity.

Sending Custom Objects:

If you want to send custom objects from one Activity to another, your object class should implement the Parcelable interface. Here's a brief example:

@Parcelize
data class User(val name: String, val age: Int) : Parcelable

Now, you can send an object of User using:

val user = User("John Doe", 30)
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("KEY_USER", user)
startActivity(intent)

And receive it with:

val user: User? = intent.getParcelableExtra("KEY_USER")

Note: When using Parcelable, don't forget to add the @Parcelize annotation and make sure you have the Kotlin Android Extensions plugin enabled in your project.

That's the basics of sending data between activities! Always make sure to handle possible null values and handle them gracefully, especially when dealing with extras from intents.

  1. Passing data between activities in Android example:

    Passing data between activities in Android involves using Intent. Here's a basic example:

    In the sending activity:

    Intent intent = new Intent(this, ReceivingActivity.class);
    intent.putExtra("key", "Hello, this is data!");
    startActivity(intent);
    

    In the receiving activity:

    Intent intent = getIntent();
    String data = intent.getStringExtra("key");
    
  2. Intent.putExtra Android example:

    Use Intent.putExtra to attach data to an Intent:

    Intent intent = new Intent(this, TargetActivity.class);
    intent.putExtra("key", "Your data here");
    startActivity(intent);
    
  3. Android bundle data between activities:

    You can use a Bundle to organize and pass multiple data items:

    Intent intent = new Intent(this, TargetActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("key1", "Value 1");
    bundle.putInt("key2", 42);
    intent.putExtras(bundle);
    startActivity(intent);
    

    In the receiving activity:

    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        String value1 = bundle.getString("key1");
        int value2 = bundle.getInt("key2");
    }
    
  4. StartActivityForResult Android example:

    Use startActivityForResult when expecting a result from the called activity:

    In the calling activity:

    Intent intent = new Intent(this, SecondActivity.class);
    startActivityForResult(intent, REQUEST_CODE);
    

    In the called activity, set the result before finishing:

    Intent resultIntent = new Intent();
    resultIntent.putExtra("resultKey", "Result data");
    setResult(Activity.RESULT_OK, resultIntent);
    finish();
    

    In the calling activity, handle the result:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            String result = data.getStringExtra("resultKey");
            // Handle the result
        }
    }
    
  5. How to pass data using Intent in Android:

    Pass data using Intent as shown in the first example. You can use methods like putExtra for primitive types or putParcelable for custom objects.

  6. Android startActivityForResult data transfer:

    Use startActivityForResult for data transfer when you expect a result. Handle the result in onActivityResult.

  7. Implicit and explicit intents in Android:

    • Explicit Intent:

      Intent explicitIntent = new Intent(this, TargetActivity.class);
      startActivity(explicitIntent);
      
    • Implicit Intent (e.g., opening a website):

      Intent implicitIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
      startActivity(implicitIntent);
      

    Implicit Intents are used for actions like opening a URL, sending an email, etc., where you don't explicitly specify the target component.