0

To pass a data from one activity to another you need to put that data in a bundle and pass it in the intent:

Bundle bundle = new Bundle();
bundle.putString(“key”, “MyDataString”);
Intent a = new Intent(Sender.this, Receiver.class); 
a.putExtra(bundle); 
startActivity(a);

However, say I have 3 activities. Each of these activities needs access to the same collection object: an array of photos.

Activity 1 needs to display the photos in list view

Activity 2 needs to display those photos in a completely different layout

Activity 3 needs to display some of the photos but can also dynamically replace which ones are being shown

As a result: all 3 activities need to share the same data object.

Do I need to keep passing the object as a bundle between each activity or is there a way to have only 1 instance of it and all 3 classes share it?

Aggressor
  • 13,323
  • 24
  • 103
  • 182

2 Answers2

0

You can just create your own class, call it session.

Then implement it like so:

 * --------------------------
 * Description
 * --------------------------
 * This class is used to store variables only until the app is completely closed.
 * --------------------------
 * 
 */
public class Session
{
    //Variables of current instance of app.
   public static String var1     = "1234";
   public static ArrayList<String> photos = new ArrayList<String>();
}

Then in other activities, just call those variables directly, like so:

String temp = Session.var1;

or set them

Session.var1 = "New Value";

or

for (String temp : Session.photos)
{
    String photoURI = temp;
    //and so on ...
}

I do this all the time... And offcourse, you don't want to put your entire app data in here, but it does save you some time in some cases.

Janpan
  • 2,164
  • 3
  • 27
  • 53
  • No. Don't do this. An `Activity` should not expose `public static` variables or methods - it's a very bad design principle for Android. – Squonk Nov 13 '14 at 22:19
  • you don't have to extend this as an activity. I changed it. I honestly don't see why it is so bad, depending on what you want to use it for. – Janpan Nov 13 '14 at 22:21
  • 1
    I can 100% see how this would work and I had considered this. I'm going to give this method more thought. In another langauge (say Obj-c) I would have a master controller object and it would be responsible for passing that object between controllers (the controllers wouldnt need to talk to each other directly to get what info they needed). Since Android seems to not cleanly allow 'Master Activities' I'm trying to figure out a clean way of passing data between activities without redundancy. Your solution is valid but for me still not ideal. – Aggressor Nov 13 '14 at 22:33
  • @Aggressor I agree with you. I have been using this to store temp variables that are synced to a remote server, one at a time with a lock mechanism, inside of an asyntask that runs in a background thread, so it works great for me for that specific purpose. I don't think at all it's the best solution, however I never considered anything else at the time and just implemented it as I needed it. I am open for a more cleaner way to achieve this. – Janpan Nov 13 '14 at 22:36
0

Actually, you have have one abstract Activity (lets say BaseActivity) and then make all other Activity's extend this BaseActivity. You can have the common variables and commonly called functions (eg: for creating AlertDialogs, ProgressDialogs, starting activities using Intents etc) in the abstract activity. That way less code for the same functionality. Since all your activities will be child classes of this parent activity class, all of them will be able to access the variables defined in the BaseActivity.

See the example from android how to create my own Activity and extend it? . Taken from the answer on same question:

public abstract class BaseActivity extends Activity {
    String commonVariable;
    @Override
    public void onCreate(bundle) {
        super.onCreate(bundle);
        setContentView(getLayoutResourceId());
        commonVariable = "Hello";
    }

    protected abstract int getLayoutResourceId();
}

public class Activity1 extends BaseActivity {
    @Override
    public void onCreate(bundle) {
        super.onCreate(bundle);
        // do extra stuff on your resources, using findViewById on your layout_for_activity1
        Log.d("TAG", "Hey I can access the common variable. Its: "+commonVariable);
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.layout_for_activity1;
    }
}

If you just have one common variable, you can go obviously go ahead and make a class and define static variable there, as @Janpan has suggested. However its a design choice and based on your situation you would want to consider what will be beneficial to you in future, as the project grows.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • 3
    But doesn't this impose a relation where none might exist? Why must all these classes inherit from a base class when all they need is a reference to a common static object? – Aggressor Nov 13 '14 at 22:22
  • You are right in a way. I guess its a matter of choice. This is an alternative design which one could consider. In short term, yes, one could just have a `Class` with `static` variables and use that as @Janpan has suggested. But in long run this seems cleaner to me as there might be many common functions eg. for showing `AlertDialogs`, `ProgressDialogs`, starting activities using `Intents` and many more which are usually used by most of the activities. Instead of defining them again and again in each activities, he would just have to define once. So less replication of code. – Shobhit Puri Nov 13 '14 at 22:29