Start two Activities simultaneously - android

Is it possible to start an activity in the background? That is, instead of showing it in the front, I want it to start in the background and want it running "under" another Activity.
Is this possible?

Are you looking for Service ?
Activity is supposed to be run in foreground while Service is supposed to be run in background
There can be only one top Actvity!

You can't start two activities simultaneously but what you can do is start the activity that you want in background 1st and inside the onCreate method of that activity call the foreground activity in such a way you can have desired activity at top and another one in background ready to be brought to foreground when required.
Your mainActivity---->open Activity1(supposed to be in background)-->Activity2(Activity2 comes FG while Activity1 goes in BG).

Related

Run activity B in the background when i start my app (A is my launcher activity)

I have two activities A & B. Activity A is launcher activity & I want to Run Activity B in the background when i launch my app. I dont want to go on activity B. I just want to run in background. Also I don't want change this activity to service.
My Question is that Can we run a activity in background? if yes then please give me some sample of code how can we run?
You cannot really run an Activity on background! When an activity is not on foreground it gets to onStop and then the system could terminate it, to release resources, by onDestroy method! see Activity Lifecycle

Start activity of another task and stay within current task

I have an application with, let say, two activities. One of the activity is already launched and after pressing "home" button it is in background. Now, from the ANOTHER application the second activity is launched. And when this second activity is finished I got to the first launched activity, not in the application that started the second activity. Is it normal behaviour? If it is, is there some way to change it? I need to stay in the application that launches the second activity, and not in the application which this second activity from.

First activity is called after coming in foreground from background

In my application, I am finishing my first activity when I move to the next activity like this:
if(className.equals("com.tritonhk.android.LoginActivity"))
startActivityForResult(in, 1);
//loadingrelative.setVisibility(View.GONE);
displayVal = 0;
Helper.IsFullSync = false;
LoginActivity.this.finish();
So that when I go in background from any other activity and come back in foreground then that activity must be called by which we went in background.
It is happening in some cases but If I remain in background for more then 10 minutes then my first activity's oncreate method is called.
It seems that dalvik is killing my application process that is why when I click on my application icon then its new instance is created and hence its onCreate is called.
Please suggest me what should be the better approach for this.
EDIT Problem solved partially. Now with android:launchMode = "standard" behaves normally but not in first attempt. I mean when I install the app and run it and went to background and come back to foreground then it does not work but from the second time it works properly.
Since you are finishing your current activity which launches subactivity means you don't want the task to retain root activity on re launch ,
you want to start from where you left ,you may use android:alwaysRetainTaskState which will retain state of task since it is useful for root activity only so you have to start your subactivity in a new task using FLAG_ACTIVITY_NEW_TASK intentfilter flag.
Do u have android:launchMode="standard" for the activity which is called while launching the app? If not add this line.

How can I launch an activity to be on top of all other activities?

I am trying to create an application that defines two activites. The first activity pretty much runs all of the time. The second activity requires the user to authenticate to use the device.
Most of the time this application works fine. However, I am having problems figuring out how to force the second activity to the top of the window stack. The code calls startActivity passing in an intent to start the second activity. The problem is that when another application is running (e.g. a web browser), the second activity is not on top. When the other application exits, the second activity is visible to take input from the user.
Here is the activity definition for the second activity that I want to have always on top when started:
<activity android:name=".Authenticate"
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden">
</activity>
It is my understanding that when start activity is called, it will put the new activity on top. Is there something that I am missing? How can I make the authentication activity come to the top when it is started?
Well AFAIK, you cannot force your Activity to stay on top all the time. If some other process (say Web Browser) creates an Activity after you have created yours, then that processes Activity will have focus and not yours.
whenever a new activity starts it is on top of the stack and if another application is running then it will be on the top of the stack and if you want your activity on top of the stack then you should stop other application being launched....Is there something I am missing then let me know..

android - How to Exit from the application

My Application have 20 activities. Here i want to implement the how to exit from the application when you click on the button(like Logout). it means if you click on the menu button any where of our application then it shows the one button. if click on that then directly comes out from the application. how to implement it.
thanks
Well naresh you can do something like that
first finish the activity from which you are closing application this.finish(); secondly and most impotantly always set a flag i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this clear top when you switch from one activity to another activity and as you know every activity is kept in stack to so this flag remove old activity from top and push new activity in top so around your whole application only one activity is kept in stack
and if this does not work put the whole application in background by avtivityname.moveTaskToBack(); this will move your whole app in back ground but only one drawback when you start your activity it will show your that activity from which you have moved back
System.exit(0);
should work, don't forget Java common functions work on android, there isn't only the android library!
As for the button being in the menu in every activity, you could create a class derived from Activity which creates and handles the menu properly, and make every other activity inherit that derived activity.
First finish the activity from which you are closing application: this.finish();. Secondly and most impotantly always set a flag i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); This clears top when you switch from one activity to another activity. As you know every activity is kept in a stack to so this flag removes old activity from the top and pushes new activity to the top so around your whole application only one activity is kept in the stack.
If this does not work, put the whole application in background with avtivityname.moveTaskToBack();. This will move your whole app to the background. One drawback: when you start your activity it will show your that activity from which you have moved back.

Categories

Resources