Sobes.tech
Junior

How can data be transferred from one Activity to another in Android?

sobes.tech AI

Answer from AI

There are several methods:

  1. Intent Extras: The most common way. Data is packed into a Bundle and added to an Intent.

    // In the first Activity
    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("key", "value"); // Primitive types
    intent.putExtra("object", new MySerializableObject()); // Serializable
    startActivity(intent);
    
    // In the second Activity
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String value = extras.getString("key");
        MySerializableObject object = (MySerializableObject) extras.getSerializable("object");
    }
    
  2. Parcelable Objects: Similar to Serializable, but more efficient for objects that need to be passed frequently. Requires implementing the Parcelable interface.

    // In the first Activity
    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("parcelable_object", new MyParcelableObject());
    startActivity(intent);
    
    // In the second Activity
    Intent intent = getIntent();
    MyParcelableObject parcelableObject = intent.getParcelableExtra("parcelable_object");
    
  3. Shared Preferences: Suitable for passing small amounts of data that need to persist between app launches.

    // In the first Activity
    SharedPreferences preferences = getSharedPreferences("my_prefs", MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("key", "value");
    editor.apply(); // or commit()
    
    // In the second Activity
    SharedPreferences preferences = getSharedPreferences("my_prefs", MODE_PRIVATE);
    String value = preferences.getString("key", null); // null is the default value
    
  4. Static variables: Not recommended, as it violates OOP principles and can lead to memory leaks. Use with caution and only for very simple cases.

    // In a static class or any other accessible static variable
    public static String staticData;
    
    // In the first Activity
    StaticDataHolder.staticData = "data";
    
    // In the second Activity
    String data = StaticDataHolder.staticData;
    
  5. Database/File: For large data volumes or data that needs to be persistent. Data is saved in a database (SQLite, Room) or a file, and the second Activity reads it.

  6. ViewModel (together with LiveData): In the context of architectural components, ViewModel is the preferred way to manage UI-related data. Data between Activities is transferred via shared ViewModel or through events on LiveData.

    // In the first Activity (Kotlin)
    val intent = Intent(this, SecondActivity::class.java).apply {
        putExtra("data_key", "data_value")
    }
    startActivity(intent)
    
    // In the second Activity (Kotlin)
    val data = intent.getStringExtra("data_key")
    

The choice of method depends on the type and volume of data being transferred, as well as persistence requirements. Passing data via Intent Extras is standard for small data volumes and direct Activity transitions.