What are the ways to starts a android activity in different process .
one I know is Adding in the manifest
android:process=":YourProcessName">
is there any other method while starting Activity using intent etc?
is there any other method while starting Activity using intent etc?
No, sorry.
Note that running activities in separate processes is usually unnecessary.
Related
I have an app that could be launched by a user or by the system (based on its main activity intent). So, in some cases there might be two instances of the app running at the same time.
Is there a way to limit the app to a single instance?
Thanks.
Add android:launchMode= "singleInstance" to your activity.
Hope it helps.
Under some conditions, my application gets started by calling one of
my Broadcast-Receivers or Services (etc), but I want to prevent the
component that is used for the intent to get started.
Why: Because I need to do some prior initialization work do run, before any
component should start. But this work can be a long running thing,
therefore I can not just execute it inside of my application subclass on the main thread.
I am subclassing the Application in my app. Therefore my approach would be
to somehow intervent the intent in the onCreate() of my application subclass and
instead, start a specific service of mine, which runs the prior-initialization
and re-calls the intent, that was intentionally used after that.
Can you image any possibly do get this done?
Thank you!
Ps.: I have a lot components that could possibly start my application. I do not want to include my condition-code inside of every component.
but I want to prevent the component that is used for the intent to get started.
Other than crashing your app, this is not possible. And, even then, the component would never be started.
Therefore my approach would be to somehow intervent the intent in the onCreate() of my application subclass and instead, start a specific service of mine, which runs the prior-initialization and re-calls the intent, that was intentionally used after that.
Short of a custom ROM with a custom framework implementation, this is not possible. You do not have access to the information that you need, nor do you have any means of stopping the component.
And, if you are in a case where a custom ROM is a possibility, move this initialization work to a core OS process (i.e., not an Android SDK app, but a standard Linux process started at boot time), and have your app use IPC to get this data.
I have an Android app with 2 primary activities. When the app starts from scratch, both activities start and run just fine. Something like: A -> B. Activity A does all of the initialization needed for both A & B. All of my local testing on real hardware and emulator, A is always created (onCreate) before B is created.
However, on my app's crash report, I see an exception which can only be explained by B being started without or before A. Is this possible? Will Android create an internal Activity without creating the other activities for my app (B without A)? Is the order of Activity creation guaranteed (A then B)? How would I re-create either of these scenarios using the emulator or real hardware?
I can easily move my initialization code to work regardless of which Activity is started first, BUT I wanted to learn how to reproduce and test before making changes. I looked through the documentation but it didn't really help.
Here is the code that starts task B when user presses "play" button:
private void handlePlayTouch()
{
Intent intent = new Intent(getApplicationContext(), PlayActivity.class);
startActivity(intent);
}
After you started Activity B, you press home button and make your application in the background. The system would kill your application if the free memory is very low. If you tried to switch to your application after your application killed, the system would try to restore your application and activity B without create A first.
You can use DDMMS's Devices view to manually stop your application, there's a red "stop process" button.Make sure that you should make your application in the background.
In your case, I suggest you using a single activity. In the on create, you can prepare everything and then do what you need. If you would like to be sure that something will be executed only when something else is finished, use AsyncTasks.
In doInBackground => do the initialisation and onPostExecute, do what you have to do after.
The onPostExecute will bee executed only when the doInBackground has finished.
http://developer.android.com/reference/android/os/AsyncTask.html
EDIT:
Your structure is not respecting a good programming practice in Android, but if it is mandatory for you to keep this structure, you should at least use "unkillable" services for the activity A. This will make your code harder to destroy as a simple activity when your app will be put on the background, but there is still a chance to be destroyed.
To understand your problem, see the android activity life cycle:
For the services using, see:
http://developer.android.com/reference/android/app/Service.html
http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/
I want to write a test case that spans over multiple activities(3 to be precise). I need to start a service which will be used by the all 3 activities. How could I start the service from my TestCase using Solo? Also after the service starts would I have to take some special steps to bind the activities to the service? Sorry I am an Android noob.
If you start your service along with your app. you will not have anything to worry about one the activity launches. Alternatively you could add a 4. activity which starts the service and then trigger this activity from Robotium prior to the "real" test.
While application will be started, I want to launch one activity if some condition is satisfied else want to launch another activity. How to do this?
Start the one, and in onCreate() check the condition. If it is not satisfied start your another activity.
you must do it handling all conditions in an initial activity, which will check your rules and start the other activitys.
I don't know much about your particular use case, but if your application has to launch different activities depending on the task it is supposed to do, you should consider having appropriate intent-filters for each, and sending an Intent that's in accordance to the intended effect. That will make the correct activity be launched depending on the Intent, which is one of the great features of Android.
But of course, if you can only decide which activity is best once you launch your app, this approach doesn't work, and you have to launch an initial activity to decide which one to call next.
If it's highly likely that one activity will be launched, you might consider launching it as the first activity and then testing whether you need to launch another instead. That's probably cleaner than having a separate activity just to make the decision about which other activity to launch.
Bruno Oliveira, Developer Programs Engineer, Google