How to import the generated activity class androidannotation - android

I just started playing around with android annotation.
I know that when I do #Eactivity, another ActivityName_ class is generated instead.
Now, in my code, i have ActivityName.this written somewhere. But when i change it into ActivityName_.this, my compiler starts to complain that it is not recognized. My question is how do I import the proper ActivityName_ ?

First of all, you do not have to change all the class names to the generated class name. In this case, writing ActivityName.this will work just fine. Actually you only have to use the generated class names for Activitys in two places:
in the manifest where you declare the Activity
in your intents, where you start an Activity (and you can use the generated Intent builder: ActivityName_.intent(this).start()
The generated classes will be available after you compile (make) the project. So please compile the classes first.

Related

Android - How to extend application class mid-project for LeakCanary

TLDR:
I have to extend the application class but don't know where to start.
Ok... I know this is a dumb one but I have to ask it.
I need to install LeakCanary and I see in their documentation that it requires me to put code in the Application Class.
I understand how to make a java class that extends the application class, but I have some questions before I do this.
SITUATION:
I have 9 classes in my app already.
The entry point to my app is MainActivity.class (figures, right?)
I have not had to extend the Application Class yet.
App name is "CST-ALERT"
QUESTIONS:
Do I just create a new Java class in my java source folder and name it the same as my overall
package name? What do I name this new class?
Do I need to adjust the manifest now to have this as the entry point
instead of MainActivity? Which I imagine will change things quite a
bit for me.
Do I have to change anything else in the manifest? I assume I will
at least have to declare it in there.
Hopefully my situation and questions will give you an idea of what I am asking.
Do I just create a new Java class in my java source folder
Yes.
and name it the same as my overall package name?
If by "it", you mean the Java package, you already have a directory for your existing Java code. Put your subclass of Application in there, unless you have a clear reason not to.
In this trivial sample app, I put my Application subclass in the same directory as my launcher Activity, which is the directory for my Java package (com.commonsware.android.button).
What do I name this new class?
That is up to you. I called it CanaryApplication. If you think you leak a lot, call it CoalMine. So long as it is a legal Java class name, it doesn't matter.
Do I need to adjust the manifest now to have this as the entry point instead of MainActivity?
Um, well, I wouldn't describe MainActivity as the entry point. Regardless, MainActivity is left alone. You need to add an android:name attribute to the <application> element, identifying your Application subclass.
Do I have to change anything else in the manifest?
No, that should suffice.

android activity in jar library

I have copied an activity class from the internet : get user input
When I had the activity class inside the project, as a class file - it worked fine.
When I inserted the class +layout file into a different project (my dialogs.jar file) I could not start the activity.
The activity (TextEntryActivity) is under "com.xyz.dialogs"
My project is under "com.xyz.thisproject"
First I got the "activity class not found" error:
"AndroidRuntime(487): android.content.ActivityNotFoundException: Unable to find explicit activity class ..."
Then I read in an article that I should insert the startActivityForResult into a try/catch and I did.
The result is :
Activity is not shown
onActivityResult is fired without any action from the user
Notes:
I put the activity in both manifests (first only in jar,then in my project, then both)
I have another class (not activity!) in that JAR file and its working fine (a OK only dialog I build with dialog builder)
My questions are:
Is it at all possible to put an activity in an external library ?
Is it ok that that the layout file is in the jar (its only a textbox with two commands ok/cancel)
Where should I declare the activity (which manifest or both?)
How should I declare the activity (I saw samples with "ActivityName" and some other samples with ".ActivityName" where should I use what method...)
Thanks in advance
Guy
I have at least a workaround:
Make an Eclipse library project with your activity (there is an is-library property in the Android properties of an Android project) and make your second project use the library project. Here is more information on that topic: http://developer.android.com/guide/developing/projects/projects-eclipse.html
Then subclass your activity and declare the subclass in the manifest. This works around the package issue.

Class extending Application in Android project library?

I have a project (in Eclipse) which I've turned into an Android Project Library so as to re-use some of the code in another similar project. I think I've shot myself in the foot however as I'm getting the error:
Unable to start activity ComponentInfo{com.test.scroller1/com.lib.scrolltest.ScrollTestActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.lib.scrolltest.resAppVars
com.lib.scrolltest is my Project Library which instantiates a class extending Application (resAppVars). In the onCreate() method I call:
mRav = (resAppVars) getApplicationContext ();
This way, I can use methods in the mRav object which would otherwise be a lot of duplicated code in other classes (such as passing a query to a generic select statement which returns an ArrayList of results).
What's the problem here? It seems I've hit a limitation in the way I've implemented the Application class.
Calling getApplicationContext() returns the Application object for the current application (i.e. the application that owns the activity that onCreate() is running inside of).
Unless you're doing something strange, you don't get to pick which Application class is used. There's even a note in the documentation for Application saying not to do this:
There is normally no need to subclass Application. In most situation,
static singletons can provide the same functionality in a more modular
way. If your singleton needs a global context (for example to register
broadcast receivers), the function to retrieve it can be given a
Context which internally uses Context.getApplicationContext() when
first constructing the singleton.
You should just create a regular shared class inside of your library project. Or if you don't have a need for the special functionality library projects offer, you can also just use a regular .jar file.
And if you need shared state, just make it a singleton. ;)
Although this is a very old post. I encountered the same problem and solved it. So I thought I'll post the solution for everyone.
It turns out that I forgot to declare the subclassed application name in the manifest file.
The android:name should point to the extended app class, even if it is defined in the referenced library.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name="com.example.lib.MyApp">
After I added that I could use the extended app with (<cast>) getApplication() anywhere in my project.

Android Application sub-class constructor not being called

I have a class in my Android application that sub-classes the AndroidApplication object. The documents say:
public void onCreate ()
Since: API Level 1 Called when the
application is starting, before any
other application objects have been
created. Implementations should be as
quick as possible (for example using
lazy initialization of state) since
the time spent in this function
directly impacts the performance of
starting the first activity, service,
or receiver in a process. If you
override this method, be sure to
call super.onCreate().
I placed a breakpoint on my sub-class's constructor and when I run my application, it is never reached. Naturally, when I call the sub-class's getInstance() method from other code it returns NULL since the instance variable is (supposed to be) initialized when the constructor is called.
Can anyone tell me what is wrong? I would assume from the docs that I don't have to create an instance of the AndroidApplication sub-class myself, or do I? Am I supposed to modify my manifest file somehow to add the AndroidApplication sub-class and if so, how?
-- roschler
I'm posting the answer here for others. Yes you need to add the name of your Application object sub-class's name to the Android manifest. For Eclipse users, the easiest way to do this is to open the AndroidManifest.xml file, select the Application tab in the manifest editor, and use the Browse button next to the Name field to find your Android Application object sub-class name and select it. The manifest file will be updated properly to register it. I just did that and it worked.
I had a problem of not having a . before my application class name.
Should be:
android:name=".MyApp"
since the MyApp class is in the package defined in the manifest.

Can an activity based application have no activity?

Assuming I have a shared activity class defined in a Library project, which does not change for any application using it and thus does not need to be subclassed, can I get a way with creating applications without subclassing this activity for them?
To better explain my question, say I have a single activity in a Library project:
public class LibActivity extends Activity {
...
}
And now I am creating an application using that Library project. Do I really need to create
public class AppActivity extends LibActivity {
// totally empty!
}
Only so that the application have its own activity to be referenced in its own AndroidManifest.xml?
Can I get a way with a minimalistic approach, in which I subclass the activity only if I need to modify the library's activity core behavior?
Here is the fully qualified answer:
Yes, an activity based application doesn't have to derive an activity from the library's activity. The application simply uses the library's activity verbatim, unmodified.
Yes, I can get a way with a minimalistic approach, in which I subclass the activity only if I need to modify the library's activity core behavior.
I have been able to verify this with an AndroidManifest.xml that is identical in both the library and the application. It would be interesting to see whether some of this redundancy can be eliminated. I will experiment with this and report back.
UPDATE: Sure enough, it is possible to create a perfectly running application in which the only activity is defined in the library and the library's AndroidManifest.xml doesn't have any <application> or <activity>! This is possible if the application's AndroidManifest.xml has them.
You can reference library Activity classes directly from your application AndroidManifest.xml. Just specify the fully qualified name like so android:name="com.example.LibActivity"

Categories

Resources