Maximum Length of Intent Putextra Method? (Force Close)

Maximum length of Intent putExtra method? (Force close)

As per my experience (sometime ago), you are able to parcel up to 1MB of data in a Bundle for IPC. This limit can be reduced if a lot of transactions are happening at a given time. Further information here.

In order to overcome this issue, I would suggest you to save your content on a temp file and pass the path/URI of your temp file to your second activity. Then in your second activity, read the contents out from file, perform your desired operations and finally delete that file.

If you want, you may also incorporate Shared_Preferences for this task - if you think handling files is cumbersome.

Maximum length of Intent putExtra method? (Force close)

As per my experience (sometime ago), you are able to parcel up to 1MB of data in a Bundle for IPC. This limit can be reduced if a lot of transactions are happening at a given time. Further information here.

In order to overcome this issue, I would suggest you to save your content on a temp file and pass the path/URI of your temp file to your second activity. Then in your second activity, read the contents out from file, perform your desired operations and finally delete that file.

If you want, you may also incorporate Shared_Preferences for this task - if you think handling files is cumbersome.

Intent.putExtras size limit?

if both activities are yours, use a decent data model. Android doesn't encourage that much to very well designed application. Or turn it differently, it allows for fast developped application and doesn't promote much of good software application principle.

The solution of @Jean-Philippe Roy (québec ?) is interesting but singleton are quite an anti-pattern when it comes to more elaborate things, namely statefull models or serviceS.

The best option is to use an application class. This class is your singleton, by nature in android. So,

  • define an application class in your manifest
  • provide a static method to access the unique instance of the application class (it is always a singleton).
  • give it a method to receive and hold your data, call it from your first activity
  • and a second one to get them back in your second activity

---Updated after @straya's answer and 18 more month of Android programming :)

The question of sharing a data structure or processes accross application, activities, views, fragments is always present at mind when building Android application. It's important to know and consider that the application scope is the right place to hold shared structure, but using the application class itself to put a data structure in that scope is not viable with regards to :

  • code quality, if all shared data structures and process are know of the application, it will quickly become bloated with accessors for all those entities.
  • there is only one global shared pool of entities, which is not find grained enough and may lead to hard to detect ways of coupling entities

I now tend to prefer using Dependency Injection managed singletons. Dagger or RoboGuice both allow to create and inject a single instance of a given class into other classes. This technique, and DI more generally offers great possibilities for good Android designs :

  • don't degrade quality of code, it is even shortened quite a lot. Use @Inject to inject dependencies and they will get injected.
  • don't give 2 responsibilities to the singletoned class : it will not handle the singleton instance creation, the framework will do it.
  • it's easier to pass from a singleton to a normal instance
  • as those singletons become normal classes with a simple annotation, they do not contain static methods anymore and this allows to mock them very easily. And that's a big point.
  • and of course, DI annotations make it very clear when a class depends on another class, helping to self document code more.

Intent seems not sent between activity

Bitmap implements Parcelable, so you could always pass it in the intent:

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("BitmapImg", bitmap);

and retrieve it on the other end:

Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImg");

Check it here : How can I pass a Bitmap object from one activity to another



Related Topics



Leave a reply



Submit