5

I have created one activity that creates user profile and stores its information like name,id, profile pic etc.

This information is unique and should be use in all activities in application.

I want to know which is best way to create a common object that stores all information and use it in all activities.

I have read about bundle and JSON but can't understand it how to use it.

Please help me as to what option should I choose. I have done a lot of reading but not sure as of now. Please help me with whats the standard thing to do and what would be better.

Shaishav Jogani
  • 2,111
  • 3
  • 23
  • 33

4 Answers4

4

You can use Application class for accessing same object across many activities.

public class TestApplication extends Application {

    //Object declaration

    public TestApplication () {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate() { 
        // TODO Auto-generated method stub
        super.onCreate();
    }

    //setter getter for object
}

Now in your Activity:

//after setContentView
TestApplication testAppObj = (TestApplication) getApplication();
testAppObj.setSomeObj(myObj);

//retrieve as:
someObj = testAppObj.getterMethodOfObj();

You must register your Application class in your manifest file just like you register your activities:

<application
        android:name="com.pkg.test.TestApplication " />

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • This ties the Activities to the Application implementation, I don't think this is a good practice. – Carlos Verdes Oct 22 '14 at 08:54
  • @CarlosVerdes : Although it normally shouldn't be necessary to extend `Application` (especially if the programmer doesn't understand the implications), the life-cycle of the `Application` begins *BEFORE* any other app component and exists until *AFTER* all app components are terminated (unless the app process is terminated forcibly). In short, there's nothing wrong with extending `Application` and maintaining references to global objects although any programmer should be aware of the implications. – Squonk Oct 22 '14 at 12:24
  • @CarlosVerdes : Your answer suggests creating a `Service`. In that case the `Activity` will be tied to the `Service`. Bearing in mind `Services` (bound or unbound, foreground or background) are more complex than extending `Application` doesn't provide a more robust solution. – Squonk Oct 23 '14 at 20:33
  • I think is better to use a service. You can add too many services in your classpath but have only one App. But it's just an opinion, I haven't said your solution doesn't work. – Carlos Verdes Oct 23 '14 at 22:08
  • Another opinion from official web. developer.android.com/reference/android/app/Application.html --> There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way – Carlos Verdes Oct 23 '14 at 22:17
  • Can you please create getter and setter methods ? – Deepak Sharma Jan 15 '16 at 13:24
1

You can create your custom Application (like others said) and then you will have a global access to this information but I think this is not a good design because your activities will be tied to this Application implementation (you will not be able to use this activities in a different App).

I suggest you to implement a Service and use this Service in all the activities.

Check the next article to create a background service which will be active for all the activities: https://developer.android.com/training/run-background-service/create-service.html

Carlos Verdes
  • 3,037
  • 22
  • 20
  • I think is better to use a service. You can add too many services in your classpath but have only one App. But it's just an opinion, I haven't said your solution doesn't work. – Carlos Verdes Oct 23 '14 at 22:07
0

Best way to implement this is to save data in Prefrences and retrieve whenever required. To simplify this you can directly store an Object in prefrences and get that object whenever required.

To save object you will require GSON library to be added in your libs folder and the you can conver any object to string and store anywhere you want like this.

Object-->>String >>> Gson gson = new Gson(); String json =gson.toJson(Object);

Getting object back

String -->> Object >>> Gson gson = new Gson(); Object obj =gson.fromJson(json string);

The above method will return in a persistent storage and you won't keep memory occupied all the time. Static variables can be freed by system on memory requirement, hence not recommended.

Akhil
  • 13
  • 3
  • This is better than Application approach but supose to serialize from/to JSON. This is good if the information should be saved when the app is shutdown. – Carlos Verdes Oct 22 '14 at 09:57
-1

The Application way is the best, but if you want to check here is the official list of common ways to do that.

frankie
  • 101
  • 5