Issue: Passing Large Data to Second Activity

Issue: Passing large data to second Activity

You are probably getting TransactionTooLargeException

As suggested by google android guide, you could use static fields or singletons to share data between activities.

They recommend it "For sharing complex non-persistent user-defined objects for short duration"

From your code it seems that's exactly what you need.

So your code in ActivitySearch.class could look something like this:

ActivityResults.data = searchList;
Intent intent = new Intent(ActivitySearch.this,ActivityResults.class);
startActivity(intent);

Then you can access ActivityResults.data from anywhere in ActivityResults activity after it starts.

For data that need to be shared between user sessions, it's not advisable to use static fields, since application process could be killed and restarted by android framework while app is running in background (if framework need to free resources). In such case all static fields will be reinitialized.

Which way is better for pass a large list of data from one activity to another activity - android?

Best way to pass large data list from one Activity to another in Android is Parcelable . You first create Parcelable pojo class and then create Array-List and pass into bundle like key and value pair and then pass bundle into intent extras.

Below I put one sample User Pojo class that implements Parcelable interface.

import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by CHETAN JOSHI on 2/1/2017.
*/

public class User implements Parcelable {

private String city;
private String name;
private int age;

public User(String city, String name, int age) {
super();
this.city = city;
this.name = name;
this.age = age;
}

public User(){
super();
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getName() {
return name;
}

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

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@SuppressWarnings("unused")
public User(Parcel in) {
this();
readFromParcel(in);
}

private void readFromParcel(Parcel in) {
this.city = in.readString();
this.name = in.readString();
this.age = in.readInt();
}

public int describeContents() {
return 0;
}

public final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
public User createFromParcel(Parcel in) {
return new User(in);
}

public User[] newArray(int size) {
return new User[size];
}
};

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(city);
dest.writeString(name);
dest.writeInt(age);
}
}
ArrayList<User> info = new ArrayList<User>();
info .add(new User("kolkata","Jhon",25));
info .add(new User("newyork","smith",26));
info .add(new User("london","kavin",25));
info .add(new User("toranto","meriyan",30));

Intent intent = new Intent(MyActivity.this,NextActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("user_list",info );
intent.putExtras(bundle);`
startActivity(intent );

How do I pass data between Activities in Android application?

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);

Access that intent on the next activity:

String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");

The docs for Intents has more information (look at the section titled "Extras").

I cant pass too large arraylist of objects between 2 activities?

This can happen very often when using intent extras to pass large amounts of data.

Your options:

  1. Define a public variable that can be accessed from the other class using variables:

You would have arrays, but my example has ints:

Sample Image

Now, you want to access these ints from another class right? Right now you are sending the int to the other activity via intent extra. Instead, you can make an object of MyClass and access them that way:

Sample Image


  1. Make a shared preferences object with the array data, and access the data from the other class.

    SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);

And to edit:

prefs.edit().putInt(INT).apply();

How to pass a value from one Activity to another in Android?

You can use Bundle to do the same in Android

Create the intent:

Intent i = new Intent(this, ActivityTwo.class);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
String getrec=textView.getText().toString();

//Create the bundle
Bundle bundle = new Bundle();

//Add your data to bundle
bundle.putString(“stuff”, getrec);

//Add the bundle to the intent
i.putExtras(bundle);

//Fire that second activity
startActivity(i);

Now in your second activity retrieve your data from the bundle:

//Get the bundle
Bundle bundle = getIntent().getExtras();

//Extract the data…
String stuff = bundle.getString(“stuff”);

How to pass large data(JSON) between activities in Android?

You should execute the transaction on a background thread.

                   realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
TrainMainDBModel trainMainDBModel = realm.createObject(TrainMainDBModel.class);
try {
trainMainDBModel.setTrainsJson(jsonObject.getString("data"));
trainMainDBModel.setAdults(Integer.parseInt(adultsValue.getText().toString()));
trainMainDBModel.setChild(Integer.parseInt(childValue.getText().toString()));
} catch (JSONException e) {
e.printStackTrace();
}

}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
Intent intent = new Intent(getActivity(), TrainsActivity.class);
startActivity(intent);
getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
});

Then you can query this object by ID in the other activity.

  TrainMainDBModel trainMainDbModel = realm.where(TrainMainDBModel.class)
.equalTo("id", getIntent().getExtras().getInt("trainId"))
.findFirst();


Related Topics



Leave a reply



Submit