I have multiple classes which extend application class - android

If I have two class of extended with Application Class. How do I handle the App? I have two Class. How do I manage the two Global Class in manifest. I have created two Module now I am integrating so it creating a problem for me.

if i have two class of extended with Application Class. How do i Handle the App?
Have one subclass the other. Or merge them together. Or delete one. Or delete both.
In first class i have done Network parsing Work. in Second class i have done Cart Controller. Both should be Global so extended with Application class
It is unclear if either of those things should be global in scope. Even if they should be global in scope, then neither of those things need to extend Application. Have them be standard Java singletons.

Only the <manifest> and <application> elements are required.
They each must be present and can occur only once. Most of the other elements can occur many times or not at all. However, at least some of them must be present before the manifest file becomes useful.
If an element contains anything at all, it contains other elements. All of the values are set through attributes, not as character data within an element.
Elements at the same level are generally not ordered. For example, the <activity>, <provider>, and <service> elements can be intermixed in any sequence. There are two key exceptions to this rule:
An <activity-alias> element must follow the <activity> for which it is an alias.
The <application> element must be the last element inside the element. In other words, the </application> closing tag must appear immediately before the </manifest> closing tag.

Related

Multiple Application name in Android Manifest

I am new to volley.I have two classes,AppController Singleton class, and ImageController Singleton class.But in Manifest,it allows only one application name.So how do I solve this?
You can try this way:
Application Class no one:
public class MyApplication extends Application {
}
Application class no two:
public class MyApplication2 extends MyApplication {
}
In your Manifest:
<application
android:name=".MyApplication2"
android:allowBackup="false"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:supportsRtl="true"/>
Only the <manifest> and <application> elements are required, they each must be present and can occur only once
According to documentation manifest file with only one application element is valid.
Try to Marge in Single Application Class
First of all, every application should have only one application class that is the concept of MVC in android.
You should remove one application class and do whatever it is doing in another one.
As per your requirement: you have AppController and ImageController application class then remove all the code of ImageController application and merge it in AppController application class. Now use AppController class in manifest.
I'm guessing you are using a Singleton as to have a class that holds your App data for the current session and\or another one for specific stuff (e.g. networking), if so, you have a couple ways of going about that:
Use ONE Application class - you shouldn't have more than one, if so - merge them.
If you insist on having 2 Singleton classes because you want to separate some functionality, you may create 2 Singleton classes, which are NOT your application class.
If you choose option 2, you should initialize(and maybe also control) them from your application class, especially to avoid duplicating a context object (that might lead to a memory leak), but make sure it's really necessary first.
since you tagged your question with the Volley tag, I'm guessing this SO thread about isolating Volley requests might help.
Hope anything here helped!

Can there be two instances of the same activity within an app?

I noticed that one of my colleagues is declaring all the member variables of an activity as static. His point is that there will never be two instances of the activity, and static member variables are more efficient than non-static. Of course, when the activity is created, he will initialize all the members variables as one would do.
I feel this is wrong. I don't think the gain is even justifiable. But I wanted to run this by you guys. Whether or not it is a good programming practice is probably beyond the scope of stackoverflow's mandate. So my specific question is if using static member variables for an activity ever break anything (besides coder's error of not initializing them properly in onCreate).
Thank you in advance for your help.
Yes, android can create 2 instances of the same activity, so your friend is wrong.
To have only one instance, you must declare the following in manifest
<activity android:name=".YourActivity"
android:launchMode="singleTask"
android:label="#string/app_name" />
Yes, you can have multiple instances on the same activity and you often want to.
For example, if I have 3 views that simply compose of a ListView. You can create 3 activities that had the same code duplicated and small changes, or one activity that handles the loading of the layout and getting a reference to the ListView and extend it to add functionality.
You can limit an activity to one instance by declaring it that way in the manifest file.

App with multiple .action.MAIN

I have an app ready and working well, and now I want to add a widget to it. The thing is, Im not sure, if I am doing this thing right, because:
my widget would have 4 buttons, each starting a different activity from the original app
Im aware of the additional neccessary initializations, this thing would cause, since 3 buttons would start 3 activities, that would skip the main activity
I did add the .action.MAIN tag to my manifest, to the corresponding activities, thus I can access them
Is this the correct way to do this? Or is there any other, recommended way? The tag that I added to the manifest:
<action android:name="android.intent.action.MAIN" />
EDIT: forgot to mention in the original post, that the widget is a completely separate app, you would have to download it separetely from the market.
I did add the .action.MAIN tag to my manifest, to the corresponding activities, thus I can access them
That's not a good idea, as nothing can then distinguish between them (such as your buttons in your app widget). Either use unique actions (good) or hard-wire in class names to the activities in your app widget code (not so good).

includeed layouts - update in multiple activities

I have an app with multiple activities and multiple layouts. However, one piece of layout is included on several activities. I also have a thread which updates this layout. However, when i switch activity it doesn't work. Since the layout is included the elements have the same ID's, shouldn't it just work? Or do I really need to fetch an object for each element in the layout and feed it into my thread in order to make it update the elements in a new activity?
You should run the update code for each Activity/View, although the XML included is the same, each is a different instance.
My suggestion is on Restart verify is there is any modification to do in each activity, a simple way is to each Activity extend a BaseActivity that has this code.
I include a layout for adverts in my app, but on each activity that uses it, the adverts need to be reloaded.
If I call an activity from one that is using the same included layout when I go back to the previous activity it's still there.
I guess this is what you are seeing....
So you can also save that data inside sharedPreferences (if it is little data and primitive objets or parceable objects).
Also you can extend the Application class and store the data there and update every activity inside the onResume() method. that i believe is the best way to handle this. and this is quite simple to do.
Ask google about extending the application class and he will provide tons of results on how to do it. its an easy way to pass data between activities and/or keep a reference to a single object which you will use throughout the app. Just be carefull to clear it when you wont need it anymore because it will stay in existance untill the application is finished() (which comes with the application extension living thru the whole application lifetime).

How do I parameterize an Activity from within AndroidManifest.xml

I have a CustomListActivity which I wish to expose two facets of the same data.
For example: I have a list of cars, and would like to list the set of colors, and the set of models. These would be filtered on mimetype from my ContentProvider:
org.acme.cars.cursor.dir/colors
org.acme.cars.cursor.dir/models
My CustomListActivity is quite involved, but general enough to be re-used, but:
the user should be able to swap between the two lists from a menu/button bar
the list is MAIN LAUNCHER intent.
the user's choice of facet (model or color) should be remembered
we should be able to call these activities via mimetype.
So, my question is:
Can I declare two activities in the AndroidManifest.xml, each one with the same class, parameterizing it, or do I need to use some switch from within the class?
Why not keep it a single activity, keep the layouts in separate XML files, and use setContentView to switch between them?

Categories

Resources