0

I must pass an ArrayList from one Activity A to another Activity B.

I did it using getSerializableExtra and putExtra methods. I already know the meaning of these methods, but I don't know if stuff that I passed using them is stored permanently in the new activity or if it is necessary to reload activity A in order to retrieve my data in B.

So the question is: how can I load my data in a initial splash screen and then use it in all my others activity without reloading the splash screen?

Ahmed Salman Tahir
  • 1,783
  • 1
  • 17
  • 26
  • http://stackoverflow.com/questions/5819238/help-with-passing-arraylist-and-parcelable-activity – Naveen Tamrakar Nov 17 '14 at 11:33
  • http://stackoverflow.com/a/24305177/1318946 – Pratik Butani Nov 17 '14 at 11:41
  • I already know these topics, that's not the point of my question. I know how to pass objects between activities, but I don't know if they will be persistent in the destination activity –  Nov 17 '14 at 11:43
  • Why don't you make your ArrayList a global variable and then you can access it from any activity? When you fetch the data in your splash, it will remain accessible until the app is running. – Ahmed Salman Tahir Nov 17 '14 at 11:53

5 Answers5

0

You can use Preference class, in which you can define its static instance. Than create variable according your desire datatype (even ArrayList). Make property for get and set of this variable.

Set the value on splash screen and get anywhere in application where you need.

Try this, if you need , I will upload code also.

Nitesh
  • 7
  • 4
0

I have written some code regarding that, it would help other activities to fetch data easily, use this when the data is not confidential,

public class HelperShared {

public static final String score = "Score";

public static final String tag_User_Machine = "tag_User_Machine",
        tag_Machine_Machine = "tag_Machine_Machine",
        tag_Draw_Machine = "tag_Draw_Machine",
        tag_Total_Machine = "tag_Total_Machine";


public static SharedPreferences preferences;
public static Editor editor;

public HelperShared(Context context) {
    this.preferences = context.getSharedPreferences(score,
            Activity.MODE_PRIVATE);
    this.editor = preferences.edit();
}

/*
 * Getter and Setter methods for Machine
 */
public void setUserMachine(int UserMachine) {
    editor.putInt(tag_User_Machine, UserMachine);
    editor.commit();
}

public void setMachineMachine(int MachineMachine) {
    editor.putInt(tag_Machine_Machine, MachineMachine);
    editor.commit();
}

public void setDrawMachine(int DrawMachine) {
    editor.putInt(tag_Draw_Machine, DrawMachine);
    editor.commit();
}

public void setTotalMachine(int TotalMachine) {
    editor.putInt(tag_Total_Machine, TotalMachine);
    editor.commit();
}

  public int getUserMachine() {
    return preferences.getInt(tag_User_Machine, 0);
}

public int getMachineMachine() {
    return preferences.getInt(tag_Machine_Machine, 0);
}

public int getDrawMachine() {
    return preferences.getInt(tag_Draw_Machine, 0);
}

public int getTotalMachine() {
    return preferences.getInt(tag_Total_Machine, 0);
}

}

Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30
  • 1
    Thank's for the code @Pankaj, but I don't understand if I can also put data of non-primitive type in shared preferences –  Nov 17 '14 at 11:50
  • you are right, it would be a lot difficult to pass the data which is not primitive. you should make flow chart and see which are the non redundant activities and pass data to next coming activities using static variable of the activities to which you want to deliver the data like that you would be sure that needed data is there and if data is not available, you can ask user to go back or take necessary action – Pankaj Nimgade Nov 17 '14 at 11:56
0

Don't use Preference Class! Preferences are only used for settings values. For passing data to another Activity use Serializable or Parcelable. Remember that all the objects which will be passed to another activity have to implement Serializable or Parcelable. So you extend the ArrayList to a custom Class which implements Parcelable or Serializable.

You do this like this:

Intent intent = new Intent(getContext(), SomeClass.class);
intent.putSerializableExtra("value", <your serializable object>);
startActivity(intent);

and receive them like

YourObject yourObject = getIntent().getSerializableExtra("value")

or look here for Parcelable

Help with passing ArrayList and parcelable Activity


Data processed in Activity A does not need to process again in Activity B. If the data is computed in A and you send it to B computed, B receives it computed already.

Here are some ways to do it right: http://developer.android.com/guide/topics/data/data-storage.html

Community
  • 1
  • 1
mapodev
  • 988
  • 8
  • 14
  • Ok, that's the way I always use for passing data from one activity to another but I don't know if data passed from one activity A to another activity B is persistent in memory. I mean, if activity A compute data and then pass it to activity B, does activity B require to recompute all the second time I call it without passing through A? –  Nov 17 '14 at 11:53
  • when you only pass the objects to another activity why do you want it as persitent? – mapodev Nov 17 '14 at 11:56
  • i edited the answer for the question of computing data – mapodev Nov 17 '14 at 11:58
  • I want data to be persistent, but I don't know if it will be persistent using the putExtra() method –  Nov 17 '14 at 12:02
  • what do you mean with persistent, for what do you need the persistent stuff? when you pass it from A to B you dont need it to be persistent. You need persitence when you want to access it from everywhere not only in B – mapodev Nov 17 '14 at 12:05
0

if your question is "how can I load my data in a initial splash screen and then use it in all my others activity without reloading the splash screen?" Than I have better solutions for you.

Create a Class Memdata.java

public class Memdata{

    private static Memdata instance = null; 
    private String userobject;

   public static Memdata getInstance(){
        if ( instance == null){
            instance = new Memdata();
        }
        return instance;
    }

   public String getuserobject() {
        return userobject;
    }

    public void setuserobject(String userobject) {
        this.userobject= userobject;
    }
}

on You Splash Screen' onCreate method, set the value

Memdata obj = Memdata.getInstance();

obj.setuserobject("hello");


Than in any activity, where you want to access this variable, just make its object and get value.

Like in MyActivity class

 Memdata obj = Memdata.getInstance();

String str = obj.getuserobject()

You can define any type of variable according your requirement.

Nitesh
  • 7
  • 4
  • the saved data is lost when the object is once destroyed. – mapodev Nov 17 '14 at 12:03
  • The objtect is just instance of class and static type. This object will be destroy when app close the application. – Nitesh Nov 17 '14 at 12:08
  • yes and if you go back to the app after a short time and want to access the object you get either a memory leak or the data of that object is lost. best approach for singletons is for example services – mapodev Nov 17 '14 at 12:11
  • :) The requirement is about send data from one activity to another. So the Intent data is also no longer when you back from the activity or go to another activity But this class allows you to use this variable until, you are using app. Otherwise if you want to use data, when user come to app again, you have to save in File or in database. – Nitesh Nov 17 '14 at 12:15
0

You can extend the base Application class and add member variables to it:

public class MyApp extends Application {

    private String appLevelString;

    public String getAppLevelString() {
        return this.appLevelString;
    }

    public void setAppLevelString(String val) {
        this.appLevelString= val;
    }
}

You will have to update the manifest file as follows:

<application android:icon="@drawable/icon"
             android:label="@string/app_name"
             android:name="MyApp">

You can get and set data like this:

//For setting
((MyApp) this.getApplication()).setAppLevelString("Test string");

//For getting
String str = ((MyApp) this.getApplication()).getAppLevelString();
Ahmed Salman Tahir
  • 1,783
  • 1
  • 17
  • 26
  • and then to access it by `getApplication()`? thats also a bad approach. – mapodev Nov 17 '14 at 12:06
  • 1
    read this: http://www.developerphil.com/dont-store-data-in-the-application-object/ as mentioned accessing data by `getApplication` is a bad pattern – mapodev Nov 17 '14 at 12:25
  • So do you suggest SharedPreference? – Ahmed Salman Tahir Nov 17 '14 at 12:26
  • I have gone through the article you shared but I would still go for the approach I mentioned here because I use if..else block. When I see that the returned value is null I fetch it again and set my application level variable again. – Ahmed Salman Tahir Nov 17 '14 at 12:34