I am instantiating an application object in the manifest file as below.
<application android:name=".MyApp"
android:icon="#drawable/add_icon_with_oval"
android:label="#string/app_name"
android:process="com.test.myprocess"
android:allowBackup="false">
I am starting a service from my application which is running in another process. This service is also declared in my manifest file as below.
<service
android:name="com.test.package.MyService"
android:permission="some.permission"
android:process="com.test.anotherprocess" >
</service>
The problem that I am seeing here is that MyApp is instantiated twice, one for my process, com.test.myprocess and another instance for com.test.anotherprocess.
Is there is a way that I can prevent creating MyApp for com.test.anotherprocess
Since the service is a component of application, it can't run without an application. The only way to have a single instance of your Application object is to remove the android:process attribute from your service.
For more details, check: http://developer.android.com/guide/topics/manifest/service-element.html#proc
Is there is a way that I can prevent creating MyApp for com.test.anotherprocess
you cannot, system will will create Application instance for each process. If you have some initialization code in your custom Application class then you can check process name and for example skip it, see code here: Is there a way to get current process name in Android.
Related
PROBLEM: Need to initialize some objects before an activity from secondary (non-base) Instant App feature is launched.
RESEARCH: In base feature we can create android.app.Application class and declare it in the base features's Manifest:
<application
android:name="com.example.MyApplicationOfBaseFeature">
As a result, android.app.Application.onCreate() method will be called before any other activity's onCreate().
But adding the same to secondary feature results in Manifest merge error:
<application
android:name="com.example.MyApplicationOfSecondaryFeature">
QUESTION: How do I do the same for a secondary feature in my Instant App?
I need to implement very specific code in the start of the application.
I mean, not in the start of the activity(onCreate() or onStart()) but in the start of the application.
I had one solution which is not good for me, which is to have a base activity called "MyBaseActivity" and then extends from it in all of my activities.
This solution is not good for me, because this solution makes me to be able to do only one specific thing in the onCreate of each activity(the specific code I talked about), which is not what I want.
I want every activity to be able to do different things according to their onCreate() func, and in addition to do the specific code that I talked about above.
Therefor, I need to access the start of the application, or that you have another solution for me.
Thank you !
The Application class, or your subclass of the Application class, is instantiated before any other class when the process for your application/package is created.
You need to extend application class.
public class AppApplication extends Application{
#Override
public void onCreate() {
super.onCreate();
//Do whatever you want
}
}
And this AppApplication class should be included in manifest file.
<application
android:allowBackup="true"
android:name=".AppApplication"
android:icon="#mipmap/ic_launcher"
I need to implement very specific code in the start of the application.
Every time when Android "gets a request" to start any of your app component (Activity, Service, BroadcastReceiver) and your app isn't running yet, it forks the app_process (a.k.a zygote), changes its name to your.package.name defined in AndroidManifest.xml, initializes an Application instance, calls its onCreate() method, then instantiates the component requested and calls its lifecycle methods (Activity's onCreate(), Service's onCreate() or BroadcastReceiver's onReceive()).
There can be only single instance of Application class which lives untill the app process dies. That said, any class instances you create within your extended Application class will also live until the app process is killed by the system.
Example: Understanding the Android Application Class
Ref: Android:- How to Make dynamic HashMap Accessible in whole application?
I have set up a CustomHashMap (just a collection of 3 LinkedHashMaps) so that I can access it from 2 Activities, but I am
unable to access it from the SecondActivity! The only way to remove red errors in Manifest.xml is to set the lines below. If I
use android:name="com.example.intents.CustomHashMap" (following the online example) I get an error saying 'is not an enclosing
class'.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher_icon"
android:label="#string/app_name"
android:theme="#style/CustomActionBarTheme"
android:name="com.example.intents.MainActivity$CustomHashMap"> //--- The system added the $ parts here.
<activity
....
protected CustomHashMap thisMap;
....
thisMap = (MainActivity.CustomHashMap)getApplication();
In MainActivity I can do things like thisMap.getKeyValue(4) but from SecondActivity only null is returned. I feel as if I am
missing something very simple here. Any ideas?
As see here android:name :
The fully qualified name of an Application subclass implemented for the application
Means need to pass class name which extends Application instead of any other component name like Activity,Service,...
See following tutorial for adding Application class in application :
Global Variable Or Application Context Variable - Android Example
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.
I wanted to know what is the best approach for sharing singleton in my android app.
I have a few activities that does not rely on each other, but I want them to have a singleton that they can all read/write.
Where should I place it?
Thanks
Your application should be a singleton. It avoids many other problem related to sharing datas or models.
In your manifest.xml :
<application android:icon="#drawable/icon"
android:label="#string/app_name" android:debuggable="false"
android:name="<class of your application see below>"
android:launchMode="singleTop">
..... all activities/services/receivers/etc... as usual
</application>
And your android:name refers to this class :
public class MyApp extends Application {
//singleton, but use onCreate more than constructor to build shared resources.
....
}//class
And then, from within any activity, you can retrieve your singleton to get shared resources by doing this :
((MyApp)getApplicationContext()).getSharedResources()....
or
MyApp.getInstance().getSharedResources()....
Having a singleton as a app is a usual design pattern in android, it is especially useful to handle device rotation (as it tends to destroy activities and recreate them) as it provides a stable entity to persist during app lifecycle.