Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I share common functions and data across many activities in a single android application

Tags:

android

I am looking for how to share functions and data across multiple activities within a single application. I researched the daylights out of it and find some ideology war between overriding the extend for the application and doing a singleton, neither of which I can find examples sufficient to make me understand. Basically I want to share data and share functions. All activities need the same functions and data so this is not one activity sharing data with another activity. It is all activities needing to have access to the same functions and data.

What I want to know is what is the way to go and how do I do it. I need to see what I need to do in my 34 activities, what the class that is going to be common looks like, and what the Manifest entry needs to be. I also need to be sure the common data area will not be closed by the OS.

This is my first Android - Java program and now find my 15,000 line, 34 activity application needs some structure. I know, should have done things differently but the app works really well with two exceptions. One is that it is structurally a mess. Two is that the fact it is a mess is making it hard to fix one behavior I would like to fix.

This is a GPS based application for racing sailboats. It is timing critical and every activity basically runs a once a second loop inside the location manager onLocationChanged function. That part is fine and I do not want to put the GPS code in one place. The problem is that most activities need to filter the data so a lot of code is copied and pasted to the activities. The filter needs history so it needs to remember a state. There are other functions that are used by several activities so these have been copied as well. Think of a function that averages the last three GPS speed readings. It needs to save some history, do its thing, and give a result. All activities need to do the exact same thing. All this works but the problem is that the averaging starts over every time I switch activities because every activity has its own filter. That gives a glitch in the data that I need to get rid of. I need common place to save the data and hopefully a common place to run the filtering and other functions that are common. If every activity can call the filter function that is using common state data, there will be no glitch across activity changes.

I would appreciate some guidance.

like image 245
Allen Edwards Avatar asked Nov 18 '13 15:11

Allen Edwards


People also ask

How do I pass data between activities in android application?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

Can an app have multiple activities?

Most apps contain multiple screens, which means they comprise multiple activities. Typically, one activity in an app is specified as the main activity, which is the first screen to appear when the user launches the app. Each activity can then start another activity in order to perform different actions.

Can one Android application access the data of another application?

Just as an app can send data to other apps, it can also receive data from other apps as well. Think about how users interact with your application and what data types you want to receive from other applications.

Does Android allow multiple concurrent activities?

Apps targeting Android 10 can support multiple activities being resumed at the same time.


2 Answers

Why you don't just make a Class with only static functions, passing needed Parameters? An example if you want to show an ErrorDialog

public class SharedHelper{

    public static Dialog showErrorDialog(Context ctx, String message, String title, DialogInterface.OnClickListener okListener, DialogInterface.OnClickListener cancelListener){
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setMessage(message).setTitle(tilte);
        if (okListener != null){
            builder.setPositiveButton(R.string.button_positive, okListener);
        }
        if (cancelListener != null){
           builder.setNegativeButton(R.string.button_negative, cancelListener);
        }
        return builder.show();
    }
}

Singletons are (from my point of view) one of the uglyest design pattern and will bite you sooner or later. Putting anything in Application requires you to cast it everytime to the Special Application class you designed. A class with only statics however is very flexible in its usage and doesn't need an instance to work.

like image 66
Rafael T Avatar answered Nov 25 '22 17:11

Rafael T


For the storage-issue:

lookup "SharedPreferences" & "SQLite" and decide afterwards which storage-type suits your needs more.

For the methods-issue:

This question is a bit more complex and there are different ways to do it. For example you could write a parent-class that implements all your globally needed questions and you let all your activity-classes inherit from it.

public class MyParentActivity extends Activity {
    public void myMethod() {
    }
}

and:

public class Activity1of34 extends MyParentActivity {
    myMethod();
}
like image 39
bofredo Avatar answered Nov 25 '22 16:11

bofredo