implementing android library project without subclassing all activities - java

I have created an Android project and set it as a library project in order to support a free and paid version of the App. The library project is set as a "...base" project and actually the paid version simply calls the base app including its Main Activity.
I need to make some changes to limit the Free version and I can easily override layout resources, but I'd like to know the simplest way to extend/override some of the Activities. Ideally, what I'd like to do is only subclass Activities where changes which limit functionality are required, but I'm hitting a few problems. My activities redirect to other activities within the App navigation and I'm finding that the error below is being generated:
03-11 13:54:10.068: E/AndroidRuntime(21264): android.content.ActivityNotFoundException: Unable to find explicit activity class {package.free/package.base.MainActivity}; have you declared this activity in your AndroidManifest.xml?
Obviously, I can add these to the manifest or I can abstract the redirects, but the 2nd option means that I need to subclass anything that can trigger a redirect or anything which can be the target of a redirect. Is there a simpler way?

I managed to get this all working as I wanted. It does mean you need to be quite specific with projects which are using a library project in terms of which object you want to instantiate (and the activities or fragments that are declared in the manifest). I posted this question purely because there's not enough information out there on how to actually implement this stuff. If there's sufficient interest I'll try to pull together a tutorial/blog to show the steps that I took to get this all up and running.

Related

How to have a Menu always visible across multiple layouts?

I have looked quite a few places but can't seem to find this specific solution. I am interested in creating a menu that will always be visible across every layout/view of the application. This menu would act much like the header/menu of a website that is fixed and always exists across all pages of the site. I assume I can simply create a menu and have it's visibility shown from initialization but I'm not sure if that's the best way. I don't want the user to have to click any other buttons to open the menu, I just want it fixed and constantly visible from the start of the app.
Thank you.
Any help is appreciated.
Fragments, they are a way to define parts of the user interface. They are similar to activities, they have their own lifecycle. They are also closely tied to the life cycle of their parent activity.
You can add them statically by declaring them in your xml manifest and in your layout file
-or-
you can add them dynamically in runtime by creating them as java objects and xml layout files and then add them using the FragmentManager class.
check out the Documentation on the android developer website
https://developer.android.com/guide/components/fragments.html
also, check out some tutorials on youtube, I like these ones especially:
https://www.youtube.com/watch?v=6GyGtCMoR_U&list=PLonJJ3BVjZW4lMlpHgL7UNQSGMERcDzHo
Good Luck!

Android application wide sensor listener design pattern?

I am designing library related to sensors and will allow other apps to integrate this library and listen for specific gestures on their own app such as shake/tilt and as a result something happen like new activity appear from library.
But integration of library should be as easy as possible.
First way letting developers create custom Application class and init my library with following way.
MyLibrary lib = new MyLibrary(appContext);
lib.startListening();
But problem here is that I have operation related to activity context like startingActivity or getting root view. This is not quite possible if I give context at Application class (can not understand activity from app context)
Other way would be implement or inject this library in every activity inside app with onCreate() method.
lib.startListening(activityContext);
and unregister onDestroy() and related methods.
lib.stopListening();
The problem is I don't like second path and love the simplicity of the first way. Is there anything I am missing in the first way to make it possible?
[Edit]: Third way would be extending custom activity class but thats hardly viable solution for me, I don't think user will integrate my library just for such small functionality and change regular Activity extend operation to my obscure custom Activity class.
[Edit]: I think Service is right way to go. But still interested in alternative patterns.

Android: Preventing long-initializing app from being killed

I've been working on an app, and it's came long great. I have a problem though, Android, being greedy with its resources, loves to kill the app when I put it in the background. This is very bad, because the app is actually intended to be an AI assistant, and having to reinitialize every-time I need to use it means that it's not going to be very helpful in a real work environment.
I investigated ways to prevent reinitialization of the AI's brain, however, none of the methods have been very fruitful. Saving the instance of its brain will not work, because the POS models that she needs to operate can't be serialized. And employing a service won't work either, because if I want to communicate with the activity via the service, I have to reinitialize it along with the activity (correct me if there is a way around this, I just notice most tutorials put service.start() in the onCreate methods)
Is there a way around this? I only need to preserve the POS models. They take a while to load in for some reason despite only being a few megabytes.
Please note that this is to prevent data from being killed. There are no background processes that need to be ran.
You need to set a Notification to tell Android not to release your resources.
See this question: How can we prevent a Service from being killed by OS? . While the question itself is not directly applicable the answers have a lot of overlap with this issue.
You should probably be using a Service if you want to run things in the background though as Activities are only supposed to run when you have a layout in the foreground. You could store your required object in the service, grab it as necessary when (re)starting an Activity that requires it and update it when your Activity loses focus.
Edit: I accidentally pasted the wrong link. Now corrected.
Also, have a look at this Android resource if you have not already: http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Android: Possible to start any activity of other apps from my own?

This is an Android noob question.
I am trying to start an activity of another apk through my own application. Now I know I can launch any other application and invoke its main activity. In many cases I'm also able to start subactivities, for example display it's settings dialogue.
However with some applications, for example Facebook or Endomondo I would get a FC everytime I try to launch some specific activity of their application.
Now I suspect that this is a permission issue and that the Facebook or Endomondo devs just don't want other applications to get access to their activities. But do I have to find out which activities I can use and which ones I can't use by trial and error every single time?
Plus: Is there any way around this dilemma? Maybe on a rooted device?
Cheers for any pointers.
As you already said you can only use activities of other apps which are designed to be used by others applications. Normally the developer of the other app define a set of intents and actions their app will be able to understand and process.
Using any other app's activity is by default not possible, this is by design of Android as every app runs in it own sandboxed process (there are some exceptions where apps can share a process).
So to use another app's activities you must know the intents it listen on. Normally this can be found in the applications website or documentation or on OpenIntents a dictionary for intents.

How to start an activity that is defined in other Android projects?

I have defined some common Activities in a library project and want to reuse these activity in my working project.
I declared my library project as Android library, use the fully-qualified name of the Activities and declare them in the AndroidManifest.xml of the new project. However, I get 'Unable to find explicit activity class' error when launching the application.
Any other configurations shall I do in order to start the Activities?
Either your activity is not in your manifest, or there is some problem with your library project causing the activity class to not be included in the APK.
You need to use Implicit Intent and Intent Filters, check this blog entry and the android documentation for more information.
I have made a very simple sample (quick and dirty but working here): http://dl.dropbox.com/u/5289718/DummyIntent.zip

Categories