Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here a compilation of <a href="http://developer.android.com/guide/faq/framework.html#3" rel="noreferrer">most common ways to achieve this</a>:</p> <ul> <li>Send data inside intent</li> <li>Static fields</li> <li>HashMap of <code>WeakReferences</code></li> <li>Persist objects (sqlite, share preferences, file, etc.)</li> </ul> <p><strong>TL;DR</strong>: there are two ways of sharing data: passing data in the intent's extras or saving it somewhere else. If data is primitives, Strings or user-defined objects: send it as part of the intent extras (user-defined objects must implement <code>Parcelable</code>). If passing complex objects save an instance in a singleton somewhere else and access them from the launched activity.</p> <p>Some examples of how and why to implement each approach:</p> <h2>Send data inside intents</h2> <pre><code>Intent intent = new Intent(FirstActivity.this, SecondActivity.class); intent.putExtra("some_key", value); intent.putExtra("some_other_key", "a value"); startActivity(intent); </code></pre> <p>On the second activity:</p> <pre><code>Bundle bundle = getIntent().getExtras(); int value = bundle.getInt("some_key"); String value2 = bundle.getString("some_other_key"); </code></pre> <p>Use this method <strong>if you are passing primitive data or Strings</strong>. You can also pass objects that implements <code>Serializable</code>.</p> <p>Although tempting, you should think twice before using <code>Serializable</code>: it's error prone and horribly slow. So in general: <strong>stay away from <code>Serializable</code></strong> if possible. If you want to pass complex user-defined objects, <strong>take a look at the <code>Parcelable</code> interface</strong>. It's harder to implement, but it has considerable speed gains compared to <code>Serializable</code>.</p> <h1>Share data without persisting to disk</h1> <p>It is possible to share data between activities by saving it in memory given that, in most cases, both activities run in the same process.</p> <p><strong>Note:</strong> sometimes, when the user leaves your activity (without quitting it), Android may decide to kill your application. In such scenario, I have experienced cases in which android attempts to launch the last activity using the intent provided before the app was killed. In this cases, data stored in a singleton (either yours or <code>Application</code>) will be gone and bad things could happen. To avoid such cases, you either persist objects to disk or check data before using it to make sure its valid.</p> <h2>Use a singleton class</h2> <p>Have a class to hold the data:</p> <pre><code>public class DataHolder { private String data; public String getData() {return data;} public void setData(String data) {this.data = data;} private static final DataHolder holder = new DataHolder(); public static DataHolder getInstance() {return holder;} } </code></pre> <p>From the launched activity:</p> <pre><code>String data = DataHolder.getInstance().getData(); </code></pre> <h3>Use application singleton</h3> <p>The application singleton is an instance of <code>android.app.Application</code> which is created when the app is launched. You can provide a custom one by extending <code>Application</code>:</p> <pre><code>import android.app.Application; public class MyApplication extends Application { private String data; public String getData() {return data;} public void setData(String data) {this.data = data;} } </code></pre> <p>Before launching the activity:</p> <pre><code>MyApplication app = (MyApplication) getApplicationContext(); app.setData(someData); </code></pre> <p>Then, from the launched activity:</p> <pre><code>MyApplication app = (MyApplication) getApplicationContext(); String data = app.getData(); </code></pre> <h3>Static fields</h3> <p>The idea is basically the same as the singleton, but in this case you provide static access to the data:</p> <pre><code>public class DataHolder { private static String data; public static String getData() {return data;} public static String setData(String data) {DataHolder.data = data;} } </code></pre> <p>From the launched activity:</p> <pre><code>String data = DataHolder.getData(); </code></pre> <h3>HashMap of <code>WeakReferences</code></h3> <p>Same idea, but allowing the garbage collector to removed unreferenced objects (e.g. when the user quits the activity):</p> <pre><code>public class DataHolder { Map&lt;String, WeakReference&lt;Object&gt;&gt; data = new HashMap&lt;String, WeakReference&lt;Object&gt;&gt;(); void save(String id, Object object) { data.put(id, new WeakReference&lt;Object&gt;(object)); } Object retrieve(String id) { WeakReference&lt;Object&gt; objectWeakReference = data.get(id); return objectWeakReference.get(); } } </code></pre> <p>Before launching the activity:</p> <pre><code>DataHolder.getInstance().save(someId, someObject); </code></pre> <p>From the launched activity:</p> <pre><code>DataHolder.getInstance().retrieve(someId); </code></pre> <p>You may or may not have to pass the object id using the intent’s extras. It all depends on your specific problem.</p> <h1>Persist objects to disk</h1> <p>The idea is to save the data in disk before launching the other activity.</p> <p><strong>Advantages:</strong> you can launch the activity from other places and, if the data is already persisted, it should work just fine.</p> <p><strong>Disadvantages:</strong> it’s cumbersome and takes more time to implement. Requires more code and thus more chance of introducing bugs. It will also be much slower.</p> <p>Some of the ways to persist objects include:</p> <ul> <li><a href="http://developer.android.com/guide/topics/data/data-storage.html#pref" rel="noreferrer">Save them to the shared preferences</a></li> <li><a href="http://developer.android.com/guide/topics/data/data-storage.html#db" rel="noreferrer">Save them to a sqlite database</a></li> <li>Save them to a file (I’d avoid this one)</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload