-1

I am getting TransactionTooLargeException while opening new activity ( with passing huge data) but I have refer few links for solution and at the end I got solution by this link I go with solution 3 (decreasing api level). It works for me.

But I want to know if there is a more convenient way to achieve this functionality or what should I do in this scenario.

Kzryzstof
  • 7,688
  • 10
  • 61
  • 108
Android Dev
  • 109
  • 7
  • 3
    Possible duplicate of [What to do on TransactionTooLargeException](https://stackoverflow.com/questions/11451393/what-to-do-on-transactiontoolargeexception) – Khemraj Sharma Jul 23 '18 at 11:44
  • 1
    *Google Play will require that new apps target at least Android 8.0 (API level 26) from August 1, 2018, and that app updates target Android 8.0 from November 1, 2018.* ... decreasing api level is not a solution at all – Selvin Jul 23 '18 at 11:45
  • @Selvin okay then what should I do for this problem ? because I want save my activities state. – Android Dev Jul 23 '18 at 11:52
  • 1
    You can save the data to a database like Realm or you can save it in SharedPreference and retrieve it in the next Activity. – Akash Amin Jul 23 '18 at 11:53
  • @AkashAmin yes you are right at your point of view to use Database and sharedPreference but at this stage I can't use those. because it need to change lot of thing in project. – Android Dev Jul 23 '18 at 12:00
  • Shared Preference is easy to use.You can save any type of data. If its json use a gson library to get the string value and then store it. – Akash Amin Jul 23 '18 at 12:04
  • @Selvin do you think is there any other solution for this problem ? – Android Dev Jul 23 '18 at 12:33

2 Answers2

0

I had this issue before, I had to choose between

1 - Remove unnecessary data from the object I wanted to pass around (in my case it had arrays full of data not needed on the destination Activity).

2 - Implement a singleton to pass around data. Imagine you are going from Activity A to Activity B:

Create PassDataSingleton.class:

public class PassDataSingleton {

    private static PassDataSingleton instance;
    private ObjectTypeYouWantToPassAround object;

    public static PassDataSingleton getInstance() {
        return instance;
    }

    public void setObjectIWantToPassAround(ObjectTypeYouWantToPassAround object){
        this.object = object;
    }

    public ObjectTypeYouWantToPassAround getObjectIWantToPassAround(){
        return this.object;
    }
}

and on Activity A, before starting Activity B, call

PassDataSingleton.getInstance().setObjectIWantToPassAround(yourObject);

on Activity B, you get the object:

PassDataSingleton.getInstance().getObjectIWantToPassAround();

PS:

  • Of course the names and types of my example can (should) be changed
  • This same code sample works with lists, just use ArrayList< ObjectTypeYouWantToPassAround> instead
Tiago Ornelas
  • 1,109
  • 8
  • 21
  • **Singleton is not a solution for passing data between Activities ...** It will obviously will not in case of ActivityA->ActivityB->background->proccess killed->ActivityB from recent – Selvin Jul 23 '18 at 11:58
0

You could store your data in a static field, then access it from wherever you want, instead of sending large datas beetween activities. Transactions are limited to 1 mb, almost nothing for data exchange. Or you could also store it in you user data folder, or sdcard as a temp file in your current activity, and read from that file from your new activity and then delete it.

Samet
  • 917
  • 12
  • 26
  • Static field is not a solution ... It will obviously will not in case of ActivityA->ActivityB->background->proccess killed->ActivityB from recent – Selvin Jul 23 '18 at 12:03
  • I meant a static field in a class that is not an activity, or else it would hold reference to the activity and leak memory. Sorry I forgot to mention. the field must not hold a referecence to a context. It really depends on what data it is, so if you do it wisely, there is no problem. – Samet Jul 23 '18 at 12:18
  • My comment is not about context leak but that it will obviously become null in this scenerio: ActivityA->ActivityB->background->proccess killed->ActivityB from recent(**static field is null and we are in ActivityB**) ... *It really depends on what data it is, so if you do it wisely, there is no problem.* **no,** obviously **the problem will exists every time** – Selvin Jul 23 '18 at 12:19
  • He could also save his data on storage like I said, it could be a possible solution for the case you describe. – Samet Jul 23 '18 at 12:36