I have a database with one row of data that will be used across a number of activities. I need to be able to keep the row id available in all activites so I can read and wri
You can use the ApplicationContext too. In your Manifest, you should have something like this :
<application
...
android:name="xx.xx.MyApp"
...>
Now, you can access to the Application from any Activity thanks to :
MyApp application = (MyApp)this.getApplicationContext();
You can put your attributes in this class, it'll be accessible anywhere in your app. MyApp must extends Application. See Manifest and Application
There are two options that I think are fit for your purpose:
SharedPreferences
: the added benefit is that your variables will kept and available next time you start the application. You can store primitive types easily in shared preferences, like your rowId
.
Application: you can subclass the application class, something like MyApplication extends Application
, declare in your manifest that you're using this class instead of the default application, and access it using getApplication
from all your activities. The added benefit is you can store anything, even a complex data structure in the application, you define the member and access methods in your MyApplication
class. For example you could store the whole row of data in your application, not just the rowId
)
Personally, I use SharedPreferences
to remember settings that I want to be saved for the user, and not having to set them again each time the application is started is nice. And I use application for all the temporary data that I want to live across all activities as long as the application is open.
I'll describe 2 ways.
1) Use a static
variable in any one of the Activities. This is the quick, dirty and lazy way. You've been warned.
2) Create your Application
class.
Create a Simple class MyApplication
that extends Application
In the Android Manifest, there should be a field for Application, make sure you choose your Class.
Typical example.
public class MyApp extends Application
{
private Object myGloballyAccessibleObject; //make getter and setter
private static MyApp singleInstance = null;
public static MyApp getInstance()
{
return singleInstance;
}
@Override
public void onCreate() {
super.onCreate();
singleInstance = this;
}
}
In your activities,
Call this
MyApp myApp = MyApp.getInstance();
myApp.getMyAwesomeObject(); //Booyaah!
Another way is to create a application class which is available for all activities. To do that, you have to extend you Manifest with
<application
..
android:name=".MyApplication" >
and create a new Class
public class MyApplication extends Application {
public int rowId = 0;
}
inside the activities, you can access the rowId by
int mRowId = ((MyApplication) getApplicationContext()).rowId;
Here you want to get mRowId
values from all activity and it is primitive types, So
Either use Shared Preferences for store data or make your member field as a static globally
, Then you can use this data in your whole application life cycle..
EDIT: Also you can use Application class as a singleton for your application and create field mRowId in this class and also make getter setter method for this field..