简体   繁体   中英

Permanent Objects passed from one Activity to another

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?

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.

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);
}

}

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

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM