Global Objects accessible throughout Android application [duplicate] - java

This question already has answers here:
How to declare global variables in Android?
(17 answers)
Closed 2 years ago.
I have an application that uses a custom class. When the app is started I populate an instance of this class in the main activity with all the data it will hold.
Basic data is then shown in a ListView from which you can select a ListView item to see further information displayed in another Activity.
Currently, I am having to pass the data that is relevant to the new Activity to it by using:
intent.putExtra("NAME", value);
I want to implement a ViewPager so the user can easily switch between ListView items. Therefore the currently use method isn't very good as I only have the data for one entry at a time and would need to get back to the original Activity to get all the data again.
Is there a way to have my class objects globally available ANYWHERE in my application? I feel my applications code is getting bloated as I am overcoming these issues in bad code methods.
I've just looked into using:
MyApp myapp = ((MyApp)context.getApplication());
but this won't work unless I can pass the context around, which I'm not sure how to do??
In C# you'd create a static class that could handle this....
Thanks
Neil

You could have:
a static class holding your data (available until the process is killed)
a local service offering access to your data
the application class holding your data
a database mechanism to persist your data (in that case you'd simply pass IDs as intent extra)
All those options are more or less valid, it also depends on what you do with your data, where it comes from, ...

I think you are looking for a Singleton object. Create the custom class that holds all the data as a singleton object and access its data from anywhere in the code. Also, as this data may not be directly related to the UI, you should not be associating it with an Activity.
HTH,
Akshay

Related

How to send data between fragments in different activities [duplicate]

This question already has answers here:
How to pass values between Fragments
(18 answers)
Closed 5 years ago.
I've been working in infoSec for almost a decade and now I'm trying to pivot into Android development. I'm completely new in the field of Android. This question has probably been answered before, but I didn't find anyone using pictures. So here we go:
I want to grasp this thing with Fragments and activities.
Activities and fragments image
Basically I want to do a simple thing; get the result from GameFragment, pass it to GameActivity, pass it to ResultActivity, and then show it in the result fragment. (Or if there is another way to communicate with fragments in different activities)
Yes there is many way below i mention :
you can use shared preferences.
you can use sqlite.
you can pass data using intent.
Links will provide brief knowledge about this topics.
You can access the parent Activity of a Fragment by using getActivity() method. To pass data between activities you can use Intent s.
The simplest solution would be use of static variable in any common Class or in secondActivity.
If you use in second receive that value using
variable = getActivity().getIntent().getExtras().getInt("key");
Then use "variable" value.
Or declare in a common Class as like
public static int variable;
Assign value
CommonClassName.variable = 1;
Then use it.

Design for survey app

I'm very new to java and want to ask for a help about design. I'm going to build a simple survey app for android in java.
Here is how I see the app:
The main page (activity_main) contain LineEdit for ID and two buttons: Login and SignUp. This activity onCreate loads all registered users and questions from DB.
Sign up page (activity_signup) contains several LineEdit fields for user info as well as SignUp button. Also it provides free ID onCreate.
Profile page (activity_profile) contains some user info (as TextView), possibly and a Survey button.
Survey page (activity_survey) contains Question, Answers and two buttons: Prev and Next.
Please mention if something might be improved.
Questions:
Should I store all users and questions as private field in class MainActivity or somewhere else? (Assume that DB is so small that it perfectly fits in RAM)
How can I modify the DB with newly created user from activity_signup? It is rather general question, like how can I access private fields from other activities preserving encapsulation?
I'd like to have a filed like private static User current_user in class ProfileActivity. But I can get the user only from DB, which is private field of class MainActivity. How can I pass the user from activity_main to activity_profile (again preserving encapsulation)?
activity_survey contain answers of different types, e.g. Yes/No; single/multiple choice (+ your variant) etc. How can I handle this in java? My idea is to create an abstract class AbstractQuestion with method fillLayout and inherit several class ConcreteQuestion (here concrete should be replace with an appropriate title) from it which contains implementation of certain type of question. Store all questions in array of AbstracQuestion's. Is it doable in java or is there more right way to do this?
Thanks in advance for any help!
Don't store such objects in Activity. You have several options for data persistence - these are listed here https://developer.android.com/training/basics/data-storage/index.html - of course that does not cover any "external" sources such as databases like Realm or Firebase but let's keep to the basics.
If your data is complex and you think that it would be easy for you to retrieve it using SQLite queries then the SQLite Database is the way to go. You can access it using ContentProvider which can be queried from any place where you have Context.
You can also store it locally on your internal or external storage with simple Serializable classes. Imagine a single object let's call it Database that is Serializable and that contains all the data you need. You could load it in your App startup (like in extended Application class) and store the reference so it won't get garbage collected. Then you can access it from a Application static method you could write to get the reference. It is probably the fastest way to implement a simple storage with fairly complex structure but that is probably not the best if your data is "big". It will increase your App start time (preferably make the load and save operations asynchronous).
If your data is simple you can use SharedPreferences to store "key-value" data. This is a little like second approach but using the Android framework to do it.
The option 2 and 3 require that your data is Serializable or Parcelable. As Android says that Parcelable should not be used for persistence but rather for communication let's skip that one.
You can either make you objects Serializable or translate them to json objects with i.e. Gson library. and store those serialized json objects. Making them serializable directly may be faster approach but sometimes keeping jsons makes more sense.
You can't and you shouldn't have to.
If you wan't to keep your data in static fields move them to extended Application class (make sure you point it in AndroidManifest.xml with specific xml parameter) and access it from there. You can get access to your Application class whenever you have Context via context.getApplicationContext() that you can cast to your custom class.
To tell Android to use your custom Application class use the following in AndroidManifest.xml:
<application
android:name=".YourAppClass"
...>
...
</application>
I am not sure if I get the 4th question right. Basically if you have multiple values you need to store your results in some collection i.e. ArrayList. Your whole questionare could be represented by a Map<Question,List<Answer>> then <- arbitrary class names (these could be enums too)

Passing references to Activity Intent

For quite some time I've had troubles passing variables from one Activity to another, and I've usually had to resolve to some pretty ugly Static-class-hacks to make it work.
Generally something along the lines of a static method that I call with the type of the Activity, along with the variables the Activity requires. These gets stored in a static variable, and retrieved in the constructor of said activity.
Like I said, pretty ugly. And there's no such thing as "myActivity.StartActivity(new Activity);". All of the overloads for StartActivity takes either an Intent, or a typeof(MyOtherActivity).
So my question is, have I completely misunderstood the concept of Activities, or am I simply missing a completely obvious way to pass arguments to them?
#Edit: The reason I want to pass an actual reference to an object, instead of simply a copy of the object, is because I'm trying to pass a View Model from an overlying Activity, down to the new Activity. And of course any changes made to this view model, should be reflected on the parent activity, which will only be possible if the the two activy's viewmodels points to the same instance.
I'm writing the app using Xamarin.Android, but the code is nearly identical between C# and Java, so answers in either those languages is fine.
The problem is that Android can kill the process hosting your app at any time (if it is in the background). When the user then returns to your app, Android will create a new process to host your app and will recreate the Activity at the top of the stack. In order to do this, Android keeps a "serialized" version of the Intent so that it can recreate the Intent to pass it to the Activity. This is why all "extras" in an Intent need to be Parcelable or Serializable.
This is also why you cannot pass a reference to an object. When Android recreates the process, none of these objects will exist anymore.
Another point to consider is that different activities may run in different processes. Even activities from the same application may be in different processes (if the manifest specifies this). Since object references don't work across process boundaries, this is another reason why you cannot pass a reference to an object in an Intent.
You can also use The Application class to store objects globally and retrieve them:
using Android.Runtime;
namespace SomeName
{
[Application]
public class App : Application
{
public string Name { get; set;}
public App (IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public override void OnCreate ()
{
base.OnCreate ();
Name = "";
}
}
}
And you can access the data with:
App application = (App)Application.Context;
application.Name = "something";
I choose to do this on the Application calss because this class is called on the App startup so you don't have to initiate it manually.
Keep in mind that variables which are scoped to the Application have their lifetime scoped to the application by extension.
This class will be Garbage Collected if the Android feels it is necessary so you have to modify the code to include this case also.
You can use SharedPreferences or a Database to save your variables in case they get deleted and retrieve them from the App class for faster results.
Don't be overly wasteful in how you use this approach though, as attaching too much information on this class it can lead to a degradation in performance. Only add information that you know will be needed by different parts of the application, and where the cost of retrieving that information exceeds the cost of storing it as an application variable.
Investigate which information you need to hold as application wide state, and what information can simply be retrieved straight from your database. There are cost implications for both and you need to ensure you get the balance right.
And don't forget to release resources as needed on OnStop and OnDestroy
I rarely use intents, i find this way better.

A basic way to have cache/data handled by manager class that my Android Activities can all access?

So what I'm wanting to do is have a few different single manager classes that handle the caching of data that I've pulled down from the internet. I want the data in these classes/objects to be accessible from different activities in my application, whereby any of the activities can manipulate data or call functions as needed.
Eg, I have a UserDatabasemanager where I will store the initial raw JSON data I pull down from the internet and an array of User objects which is a distilled version of that JSON data.
My app at various times will pull data down from the web and will update the User array as data changes. Eg, initially pull down a list of users that fit a criteria. So I then cache all the usernames and IDs into the User objects. Should the application then focus in on a particular user page, it will then download that user's description text and store the new description into User object. (All of which I think I cant do currently with my knowledge of programming)
I've been reading up on sending objects via Intents with Parcelable and Serializable which seems a bit of a round about way of doing things for shared data between Activities.
How to send an object from one Android Activity to another using Intents?
Passing data through intent using Serializable
Since my intention is to use these classes/managers/objects (whatever we want to call them) as single instances I'm not too fussed about following encapsulation to the letter as their job is to provide and update my cache data based on the needs of the Activities.
I'm still very green with all this Android/Java stuff, but it seems this sort of thing would be fairly straight forward in C/C++ with pointers. It seems that even if I was programming directly with Java I could pass around references to objects fairly easily, but the structure of Activities seem to be in such a way that it doesn't lend itself to easy passing data around in my own program?
One easy (but not great) way to achieve this is to use the Android Application class to store (the singleton) instances of your managers. You can always retrieve a reference to your application by calling getApplication() on any activity.
You need to provide your own implementation of the Application class so that you create the managers in the onCreate() method. You should also create strongly typed getters for your managers.
Also, consider what you need to do for the system callbacks the Application may receive such as onLowMemory().
So what I ended up doing in the end is making a Globals.java file with the following contents
public class Globals {
public static ArrayList<User> userObjArray = new ArrayList<User>();
//Weird magic class stuff
private static Globals instance;
private Globals() {
}
public static synchronized Globals getInstance() {
if (instance == null) {
instance = new Globals();
}
return instance;
}
Then whenever I wanted to use data from the Globals I would do something like...
Globals.getInstance().userObjArray
This seems to be working exactly how I want it to. Its not great for encapsulation, but that wasn't the goal for what I needed to get done.
I think you should use android service and create binders in your activities.
http://developer.android.com/guide/components/services.html

How to create a global variable in android? [duplicate]

This question already has answers here:
Android global variable
(14 answers)
Closed 8 years ago.
In my android application I need a place for putting the variable member id .
The problem is , it is getting from the online API and I need to find a way to store / retrieve it
I have tried to put it in a custom class , but the problem is , it will lose if I kill the activity, I have also know that there is a way to extends the application.
So I would like to know what is the best way to store global variable?
I have to implment:
Save the variable on onSaveState
Save it on sharepref
Save it manually
Retrieve it manually
Thanks
Update:
Thanks for reply. If I have just 3 variable (simple data e.g. a boolean , a phrase ), and I need it after app restart , should I simply use share pref to store it? What is the drawback of it? e.g. Will it harmful to the performance? thanks
You can create the global variable in android by declaring them in the class which extend the Application class.
Something like this.
class MyAppApplication extends Application {
private String mGlobalVarValue;
public String getGlobalVarValue() {
return mGlobalVarValue;
}
public void setGlobalVarValue(String str) {
mGlobalVarValue = str;
}
}
MainActivity.java
class MainActivity extends Activity {
#Override
public void onCreate(Bundle b){
...
MyAppApplication mApp = ((MyAppApplication)getApplicationContext());
String globalVarValue = mApp.getGlobalVarValue();
...
}
}
Update
This hold the value until your application is not destroyed. If you want to keep your values save even after your application instance destroy
then you can use the SharedPreferences best way to do this.
Learn about the SharedPrefernces from here : http://developer.android.com/reference/android/content/SharedPreferences.html
If you need a global variable, which is shared between multiple activities and survives their lifecycle changes, but is otherwise not persisted and would not survive an application restart, the method suggested in the first answer (extending Application and putting it there) is ok.
If you need a persisted global setting, which should also survive an application restart, then you should use one of the other methods suggested. SharedPreferences is probably the easiest to use.
See also this Stack Overflow question: What's the best way to share data between activities?
You can store it in a SharedPreferences, and create a sharedpreferenceHelper class to retrieve/store it.
Its always better to store values which you want to use multiple times in one of the following ways:-
Shared Preferences
Singleton classes
Write it to a file
SQLite Database
Store on the web-server

Categories