1

Is there any disadvantage in transferring values from Activity A to Activity B with static fields of a third class instead of the ExtraBundle?

In my application, sometimes I have like 15 - 20 values that I need to transfer between two Activitys. In my oppinion, it is more lucid solving this with static fields from a sort of TransferHandler.

At the moment, I have one disadvantage in mind: When a value is not put into the Extras before starting Activity B, I will get an Exception. Solving it with static fields, it it possible that I forget to assign a value, and if that value was assigned before from somewhere else, it might be that a wrong value is used in Activity B. Nonetheless, I think this is a "programmer problem" and not a "program problem". So are there any further minusses or am I free to choice a way? How's with the performance of the two variants?

Valentino Ru
  • 4,964
  • 12
  • 43
  • 78

4 Answers4

4

First of all, if you plan to use static values, you should use your Application class to do this (because Android system assures you that it is a true singleton)

Thus, you can store your datas in attributes of your custom Application class, and use specific methods to store and get these values. This will ensure you can't "forget" any values.

Also, with 15-20, I will strongly advice you to make a specialized POJO class to store all this...

Orabîg
  • 11,718
  • 6
  • 38
  • 58
  • Yep, he should create a class for the 15 values and then implement Parcelable on it. – dmon Nov 18 '12 at 15:19
2

I think the biggest disadvantage with using static classes for passing information in android is that static fields and objects can be cleared by the system at any time. That means that any static non final value can ALWAYS be null.

So it will probably work fine most of the time, but if you don't make sure to handle these null situations and your users start using your app they'll get a null pointer exception crash once in a while because the system decided it needed that memory stored in those static fields.

The best way for passing data between activities is by my opinion by using Intents, see here for a good example. Alternatively use a database or the the sharedpreferences.

Google also have a good read about pass data between Activities/Services here.

Community
  • 1
  • 1
Warpzit
  • 27,966
  • 19
  • 103
  • 155
0

You cannot use a third class to transferring values form one activity to other. Here is the problem with it. You create one object in the activity-a then you store some values into it. Then after for using the values you need to create one more object in the activity-b then the object created in activity b will not be having the values you assigned in activity-a.

NewUser
  • 3,729
  • 10
  • 57
  • 79
-1

You can use SharedPreferences class to store valuo of variables:

SharedPreferences settings = getSharedPreferences("shared_pref", MODE_WORLD_READABLE);
    SharedPreferences.Editor editor = settings.edit();
    // With editor you put data
    editor.putString(firstName, "John");
    editor.putString(lastName, "Smith");
    editor.commit();

You can access this data in all activities:

// With settings you access to data in different activities 
SharedPreferences settings = getSharedPreferences("shared_pref", MODE_WORLD_READABLE);

String firstName = settings.getString(firstName, null);
String lastName = settings.getString(lastName, null);
Juraj Ćutić
  • 206
  • 2
  • 10