Android app pass data between activities

Android pass model Objects class from intent

Passing Data types like String, Integer, Float, double is quite easy in android so we don’t need any special class for this. But for Android pass model from intents using Serializable from intents we need Serializable class.

What is Modal Class

Model can be applied to a class which represents your application’s data model, and will cause instances of the class to become observable, such that a read of a property of an instance of this class during the invocation of a composable function will cause that component to be “subscribed” to mutations of that instance. Composable functions which directly or indirectly read properties of the model class, the composables will be recomposed whenever any properties of the the model are written to.

What is Serializable?

It is a standard Java interface. So you can just implement Serializable interface and add override methods.

There are pros and cons of this class. It is comparatively slow and easy to integrate.

To immplements simple Data types we can just pass ‘Xyz’ by key ‘name’.

Intent intent = new Intent(MainActivity.this, DataActivity.class);
intent.putExtra("name","Xyz");// for string variable 

And in data activity to fetch the data by key ‘name’.

Intent intent = getIntent();
intent.getStringExtra("name");// to fetch data

But for passing objects or model class we need serialization.

So let’s get started on how Android app pass data between activities.

For this section I have created MainActivity, DataActivity and UserModel to achieve the task.

MainActivity.java

To send the model class from MainActivity to DataActivity class by using Serializable class.

package com.example.sampleproject;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ((Button)findViewById(R.id.click_me)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserModel mUser = new UserModel();
                mUser.setId(1);
                mUser.setName("Xyz");
                mUser.setMobile("99919290XX");

                Intent intent = new Intent(MainActivity.this, DataActivity.class);
                intent.putExtra("name","Xyz");
                intent.putExtra("user", mUser);
                startActivity(intent);
            }
        });

    }
}

DataActivity.java

To fetch the model class from main activity.

package com.example.sampleproject;

import android.content.Intent;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.util.Log;
import android.view.View;

import java.io.Serializable;

public class DataActivity extends AppCompatActivity {

    private String TAG = "DataActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        Intent intent = getIntent();
        if (intent != null) {

            Serializable s = intent.getSerializableExtra("user");
            UserModel mUserModel = (UserModel) s;
            if(mUserModel!=null) {
                int id = mUserModel.getId();
                String name = mUserModel.getName();
                String mobile = mUserModel.getMobile();
                Log.d(TAG, "id: "+id);
                Log.d(TAG, "name: "+name);
                Log.d(TAG, "mobile: "+mobile);
            }else {

            }
        }
    }

}

UserModel.java

Model which is shared between the MainActivity and DataActivity class.

You can also add child model or list of UserModel class just you have to implements that model by Serializable.

package com.example.sampleproject;

import java.io.Serializable;

public class UserModel implements Serializable {

    private int id;
    private String name;
    private String mobile;


    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getMobile() {
        return mobile;
    }
}

Output of Android app pass data between activities:

The output of this code will look like this.

id: 1.
name: Xyz.
mobile: 99919290XX.

You can also see this post where I have passed a model class between activities by using intents from here.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Tagged : / / / /

Leave a Reply

Your email address will not be published. Required fields are marked *