I'm calling startActivity from a method in my Application class, but neither the activity's constructor nor its onCreate method is being called. The Application method is being called when a menu option (in the first activity instance) is selected, so the first activity is on top. I don't have a launchMode set for the activity (i.e. it is "standard"), and onNewIntent is not being called.
However, if I copy the .java file (+ rename the class and copy the Activity entry in AndroidManifest.xml), then call startActivity with the new class the second time, it works just fine. I need to create an unknown and possibly high number of these activities, so this is not a solution, especially when you consider all the duplicated code.
How can I force Android to create multiple instances of this activity?
Related
I have a class in my app that extends the Application class. I have declared/named it in the Manifest.xml file. It basically initializes some stuff but more importantly gives me access to a serial port on the machine I'm programming on. Here is my question. I have an activity where I subclass this Application class. The Activity, lets call it Activity A initializes a variable that is declared in it's superclass (the one that subclasses Activity).
When I create an intent in Activity A meant to send the user to a different Activity, after calling startActivity(intent) I call finish(). Will that completely erase Activity A from memory, or, because in Activity A I reference a variable declared in the Application class, will this Activity A remain in memory
Nope, actually when we use superclass properties in sub class it won't be more in memory. So when ever we call for finish() for activity. All declared variables and all references related to that Activity will be destroyed (removed from memory).
I have an Activity that has many fields (class fields/members). I want to set these fields by calling other activities (with startActivityForResult) but the field values are not kept. It looks like each time I return from another activity to my original one, a new instance is created and the fields are re-initialised (the onCreate() method is called each time I return from another activity, also onDestroy() is called on the original one each time I load another activity with startActivityForResult; I am never calling finish()). I hope I explained my problem well.
Any suggestions welcome.
Thanks,
Silviu
Make sure all your fields have unique id's as Android uses these id's to retain the field values when it redraws the activity.
Have you set android:launchMode="singleInstance" in the Android Manifest for the Activity where you have the startActivityForResult code?
If so remove that launchMode as it as some known issues with startActivityForResult.
I have a MapActivity subclass and I want to preserve the stack, but I can't keep multiple instances of a MapActivity in the same process. So I have come up with 2 schemes to achieve this:
Pass the state of the MapActivity along with any intents it fires and then let the activities that get switched to reconstruct the MapActivity by sending an intent that recreates the activity. Additionally, the MapActivity would be set so that intents only ever create a single instance of this activity at a time. This approach is flawed as there are multiple exit paths from this activity so all of them would need to be changed to support this.
Replace the MapActivity with a mock activity that does the recreation of the activity in it's onResume() method or something and then the activities you switch to can remain blissfully unaware of this issue. The problem with this approach is I am unaware about how I should go about creating this mock activity and also fire an intent to start the activity I want to switch to.
So my question is this is there a better way to do this and, if not, how would I go about doing option 2, if it is possible?
EDIT: One possible way to do option 2 is to make the mock activity a waypoint that starts the target activity for you in it's onCreate(). But then one just has to be careful that if the onCreate() gets called again because the activity is being reconstructed, that one doesn't start the target activity again. This can be done by checking that savedInstanceState is null.
You should use SingleInstance attribute in the manifest file, this will bring the earlier launched instance to the top of the backstack
<activity android:launchMode="singleInstance"/>
My launch activity starts up another activity whose launch is set to single instance. In this 2nd activity, I have a public method. I then start up a 3rd activity and that activity needs to access the public method in the 2nd activity. I don't want to use startActivity and pass it extras because I assume the onCreate will get called (or am I wrong?) and I need to avoid the 2nd activity from reinitializing itself.
When an activity is started using startActivity, is it possible to gain access to the underlying class instance itself and simply call the method?
I actually came up with a simple solution. As a matter of fact you can access the underlying class of an activity. First, you create a class that is used to hold a public static reference to activity 2. When activity 2 is created, in its onCreate method you store "this" in the static reference. Activity 2 implements an interface with the methods that you want available to any other activity or object. The static reference you hold would be of a data type of this interface. When another activity wants to call a method in this activity, it simply accesses the public static reference and calls the method. This is no hack but is intrinsic to how Java operates and is totally legitimate.
It is not a good idea.
As I can understand method from second activity is actually not connected to particular activity while you want to call it from another one. So carry the method out to other (non-activity) class (maybe static method) and use it from both activities.
It's not directly possible to gain access to activity object started using startActivity (without using some hacks). And frankly you shouldn't even trying to accomplish this.
One Activity component can cycle through several Activity java object while its alive. For example, when user rotates the screen, old object is discarded and new activity object is created. But this is still one Activity component.
From my experience, when you need to do things you described, there is something wrong with your architecture. You either should move part of activity's responsibilities to Service or to ContentProvider, or use Intents, etc. Its hard to recommend anything more specific without knowing more details.
No there is no way to pass a reference via startActivity() however you can use some sort of shared memory to keep reference to your Activity. This is probably a bad design. However passing an extra with your Intent will not cause onCreate, that is completely related to the lifecycle.
I need to start android application not with Activity^ but with some controller class that will start some activity
Is it possible?
I'm not sure if I understand your question correctly, but an Android application is built up by four "components" as mentioned in the "Android Application Fundamentals", http://developer.android.com/guide/topics/fundamentals.html (no, you don't need all four of them make your application work).
The most common way of starting an application (and actually the only one I've been in touch with) is to define an Activity in your applications AndroidManifest.xml file as described on the link above. NOTE! that an Activity doesn't have to define a UI; you are not obligated to call the "setContentView()" function. Hence your "controller class" can extend Activity and be the very Activity you define as the start-up Activity in your manifest xml. You can then call "startActivity()" function with parameters to start any other Activity, whenever you see fit, from your controller class (this is also described in the link above).
Hope this helps.
Either create a GUI-less activity without calling setContentView() or use a BroadcastReceiver that accepts launcher intents (action=MAIN, cateogry=LAUNCHER). In Activity.onCreate or receivers callback method you can place logic which will invoke the actual activity of choice.