RaagaSudha
RaagaSudha

Reputation: 397

How to pass data using shared preferences in android?

I want to pass a string value from one activity to other activity in android. I dont want to use intents to pass the data. How can I achieve the task using shared preferenced?

I found a very good example in http://www.how-to-develop-android-apps.com/using-shared-preferences-in-android/. But can we pass the value without using intents in it?

Please help me regarding this..

Upvotes: 0

Views: 2413

Answers (3)

codeskraps
codeskraps

Reputation: 1501

Another way to pass information between activities is using an application class.

Create a new class which extends application. Add the name of the application class on you manifest like so:

 <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:name=".YourApplication" >

This class will get first created before any activity and it a live will you application is running. You will need to override the onCreate method. Create you variables on you application class and to access you application from your activity you will need to do the following:

YourApplication app = (YourApplication) getAppliation();

Just thought to mention this just in case you didn't know. Shared preferences is good for saving permanently primary variables.

Upvotes: 1

dsafcdsge4rfdse
dsafcdsge4rfdse

Reputation: 258

If all you need to do is pass data, why don't you use store the data in the Application class and use getApplication() from each activity (first to set and later to retreiv the stored data). It's much faster than with SharedPreferences.

Upvotes: 1

Heinrisch
Heinrisch

Reputation: 5935

Just read about SharedPreferences and the use it. Its like saving things to a file, open the file with the same name in the next Activity.

Upvotes: 1

Related Questions