Android - Start a new activity while the application is in the background - android

The first activity of my application is a splash screen, where some animation is displayed while some loading occurs in a background thread using AsyncTask.
Once the loading in the background is done, I want to start a new activity. What is the correct way to do that ?
Start a new activity directly from the onPostExecute method of the AsyncTask class.
Check if the current activity is displayed before starting the new activity :
If the current activity is display, start the new activity.
If the current activity is NOT display, use a flag (boolean) and check the flag during the onResume method in order to start the new activity there.
My main concern is :
If my application went to the background (due to an incoming phone call, a home key press, ...) and the background thread (AsyncTask) finished executing, and a new activity is started from the onPostExecute method while my application is still in the background : What happens ?
Will the new activity start directly as soon as my application is visible again ?

I faced a similar situation: my application went to background and after some time the app started an intent displaying another activity. However, the app's behavior now depends on the os that it's running:
on pre 4.4 devices the app silently opens the new activity and remains in background. When the user resumes the application, he is prompted with the second activity already.
on 4.4 devices (tested on 4.4.2 and 4.4.4) the app opens the second activity, but after 3-4 seconds, the app pops to foreground, interrumping the user.
Hope it helps anybody. I'm still looking solutions for the second case in order to prevent the app from popping to foreground.

From my experience i am answering your question
Question1
If you using AsyncTask you have to start new activity in OnPostExecute(). In my experience this is the correct way of doing it.
Question2
When ever your press the home key or receiving phone call. Your activity will go in to background until you exit the app by pressing back button(at that time your app is exited). So when you come back to your app your new activity will be visible if background process get finished. Otherwise you will see the start splash screen.

I think that it is dependent upon the OS of android. It has defined some built in priority model for each of the components.
Look at the answer given by commonsware.
change process priority in android
this gives brief idea about components priority.

Related

Instance of same activity creating every time app come in foreground from background without destroying previous one

I m facing this issue of creating multiple instance of same activity when app comes in foreground from background. Every time app goes in background then comes in foreground after sometime, new activity created without destroying old so the count increases by 1 every time.
while debugging with android studio, it is found that instance of application shows dead after remaining for sometime in background, after this dead state when app comes in background this issue happens.
Launch Mode is SingleTop with ClearTop
Logs in screenshots are stated as :
in activity oncreate ----> in oncreate function of activity
in oncreate ----> in oncreate function of fragment attached to activity
in onViewCreated ----> in onViewCreated function of fragment
For screenshots click on links below.
app open first tym and moved to background
Dead state is showing instead of app running in background, when it brought to foreground, new Activity created. and look at screenshot below
after bringing in foreground

How to launch an Android application under current application?

I have two Android apps that communicate with each other through BroadcastReceiver. But if the second receiver application wasn’t started, the first one just sits there... The second application, once started, needs to continue running indefinitely, but not interrupt the first application by popping up over it. So I need a way to start the second application through the first one, BUT the screen has to continue showing the first application.
Through the first app, I check for presence of the second application, and if it's found and is not running, I start the application:
Intent SecondApp = getPackageManager().getLaunchIntentForPackage("com.example.app2");
startActivity(SecondApp);
But this brings up the second application to the screen ontop of the first one. So I need a solution to ether
1) Start the second application under the fist one
Or
2) Put the current first application immediately one on top of the second app
What’s the best solution here, and how to do it?
I assume that your second application has a service running in background and when you do the startActivity(SecondApp); pass some parameters with the intent, and in the second app read this parameter and just call finish after starting the service if that parameter is present.
Thank you guys for your help. The command finish() surprisingly does get rind of the UI, but doesn’t kill the application.
There was still the issue of only “minimizing” it when I start it from scratch through the first app, and run the second application normally in all other cases. So I added a boolean flag to the first application intent:
Intent SecondApp = getPackageManager().getLaunchIntentForPackage("com.example.app2");
SecondApp.putExtra("StartMinimized",true);
startActivity(SecondApp);
In the second application onCreate I put this code in:
Intent glob_intent = getIntent();
boolean startmini = glob_intent.getBooleanExtra("StartMinimized",false);
glob_intent.putExtra("StartMinimized",false);
if(startmini)
{
finish();
Toast.makeText(this,"L",Toast.LENGTH_LONG).show(); //Inform that app started
}
Now when starting the first application, the second application only flashes a white screen for a moment. I think this is good enough.
I found that substituting finish(); with super.moveTaskToBack(true); also works, but can cause glitching later. This way I personally started the second application twice, or caused the introduction dialog to not display when making the application one instance only.

Start an activity from any other app

I'm designing an app composed two activities. The first one always run, and is asked to trigger a second one when some stuff happens. This works fine with the standard code used for running activities:
Intent myIntent = new Intent(this, allarme.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
however, the activity in allarme.class is not started if i'm using another app (i.e. gmail),
whilel it works perfectly from home or when the screen is locked.
I'm sure that the first activity is still running, it's just that the second action is not triggered.
Should I change anything in the manifest file to fix this?
I'm not really clear about what you want.
but I guess you want to run two activities simultaneously, am I right?
while one activity run in background and one activity update the UI.
always keep in mind that Android architecture only allowed one activity to run at the time. If you want to pass the data from Asynchronous Task, you can call the method from that class and let your handler 'recent' class update the UI.
You can create a background service that always run at background even if you are using another app or phone is locked. Here is link for more help :
http://developer.android.com/training/run-background-service/create-service.html
Thanks everybody, I think I solved it.
Basically, I found out that an activity already running can be brought to focus when re-started with a basic startActivity. Then I can immediatley switch to the new activity without losing the focus.

Issue with Activity killed by System if No Longer Visible in Android

I have There Activities(A,B,C) within my Application.When i start the Application
Activity A:
A:onCreate()
A:onStart()
A:onResume()
Using intent i am calling Second Activity(A -> B):
A:onPause()
B:onCreate()
B:onStart()
B:onResume()
A:onStop()
Then I click the "Home" button So the App goes to background:Now
B:onPause()
B:onStop()
After 1 or 2 hour later Again i go to home page within my device and Click the App icon it runs like:
B:onDestroy()
A:onRestart()
A:onStart()
A:onResume()
But i need to go which one Activity i quit like this,
B:onRestart()
B:onStart()
B:onResume()
I have read some articles it says like that activity killed by the system because of no longer visible.Is there any possible to fix my issue...
Thanks in advance...
You may be confusing two different things here:
Android does not kill an activity if it needs memory. What it does is that it kills the whole process that the activity is running in. In general that means that Android kills all of your activities in this situation. However, it remembers the activity stack and when the user returns to the application, Android will create a new process and then recreate each activity (in turn, as needed). It starts by recreating the activity that was on the top of the activity stack (ie: where the user left the application).
Android assumes that if the user leaves a task for a long period of time (I think this is something like 30 minutes) then he is no longer interested in that task and there is no point in remembering where the user was in the activity stack of that task because he probably doesn't care anymore. In this case, what happens is that when the user returns to the task (or restarts the application that was on the top of the activity stack in that task) Android simply clears the task back to the root activity. This has the effect that it looks like the application is starting all over again. This is the desired (and documented behaviour).
What you want to do is prevent Android from clearing the task in situarion #2. You do it by adding
android:alwaysRetainTaskState="true"
to the <activity> tag of the root activity (ie: the activity that starts your application, the one with ACTION_MAIN and CATEGORY_LAUNCHER).
I don't believe this is something you can surely control. If your activity is in background for a lot of time and meanwhile other applications need memory, the system will kill your activity to free memory.

Running a thread while Android app is in foreground

I have an Android app. I have a main activity, that has a button. When the button is clicked, another activity comes to the foreground. The thing is, I want to run a background thread that polls updates from the server. However, I want it to run only when the app is in foreground (either the main activity or the second one), and to stop polling when the user clicks the Home button or clicks the Back button till it's going back from the main activity.
But how do I know if the app is still in the foreground? I can catch the onPause of the main activity, but it's called also when I'm launching the second activity.
So how do I know when the app is in background?
Thanks
You should make a Service for the work you are doing in the background.
For stopping it when you click the Home or Back button, just make a listener for them and stop the Service when either one is pressed.
Seems easiest to me that each activity polls. Is it super important that it can poll when it is between the two activities? Otherwise you will have problems about knowing who is in front or not.
You can have a singleton with reference counting.
You main activity should add the first reference on it's onResume and from now, upon calling for every new activity (startActivity for example) you should add a reference.
Each activity should decrease the reference counting on its onPause.
Another option is to use services: Services

Categories

Resources