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.
Related
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.
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.
I am pretty new to Android development .
My problem is a bit tricky one.
I want to develop an application using 2 activities.
1st activity has a button . On clicking the button I want activity 2 to get started as follows:
Activity 2 will come to foreground for 2 seconds and then goes to background for 8 seconds , after which it will again come to foreground for 2 seconds and then again goes to background and the process continues.
Meanwhile both activities should continue their respective tasks.
For ex. We can have a Activity such as Music player which is playing music and another activity named Activity 2 which is downloading some files.
I have tried many things ranging from using Intents to minimizing a activity and displaying notification on notification bar. The problem with notification bar is that it on resuming the activity using notification bar it is always calling OnCreate() method thus again starting the activity.
I am able to start a activity on button click but don't know how to minimize it and then pop it up in same state .
I am using services in background for timing delays .
Please Help and share your solutions to this problem.
I have solved the problem.
The solution is to modify manifest file as :
add to main activity code
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
add to child activity code
android:launchMode="singleInstance"
android:clearTaskOnLaunch="true"
Its works perfectly for my app :)
thank you all for your kind contribution
Maybe u found solution for this issue as u said, but in any case, I would recommend reading some things.. It will help you to better understand that:
There is no such a thing as minimizing activity (Activity Lifecycle),
Activity/Intent Flags and lunching modes
Tasks and back stack
What is service used for,
What are AsyncTasks,
How to use Handler
Oki.. I made some effort and organize links for you. I know it seems a lot of material, but you don't have to run to read them in next 15min.. Take it easy, read, try, learn. Take this as advice, cause once u get used to do things in a wrong way it will be much more difficult to correct them later..
Hope you find it useful.. ;) Cheers
moveTaskToBack(true); moves the whole task to back i.e. it moves both activities to back . Then the problem will be to move the activity to foreground which I don't know.
Have you tried using
this.moveTaskToBack(true);
I'm not sure if this is what your looking for or not. Heres the Documentation as well!
I describe my project background.
It have many Activity,and have login Activity,it is means i need save some msg.
I Need:
catch the all unCaughtException,save the excetionInfo. and than recovery activities if it is can do .if can't recovey activity,we should close the Application friendly and ReStart with loginInfo。
I Have Done
I use Thread.UncaughtExceptionHandler,can catch the exception,but i can't recovery activities or close the Application friendly。
Have tried
I use the
1.ActiviryManager.restartPackage
It can close the app effective,but can't restart.
2.android.os.Process.kill(android.os.Process.myPid())
It can't close my App effective. the same as System.exit(0)
3.I try to finish all Activity,
It is close App effective,i use alarm to restart App, but I find that it is seem that my app can't complete close before。
My English is poor,Here of heartfelt apology。if you have some idea,please tell me ,i thanks u so much。
For finishing activities use 'Finish()'.
What I would suggest, instead of directly restarting the app, just don't finish the main Activity and use a simple intent to the next activity.
And whenever you feel like 'restarting' the application, you just finish the current activity without using any intent; by doing so, the only activity in the stack will be the main activity (the only you havent finished).
Just put any extra code on the 'onRestart()' method and you are good to go!
PS. I hope I understood properly.
this might be an easy question but i have a notification that when clicked open up the stock messaging app
arg1.setClassName("com.android.mms","com.android.mms.ui.ConversationList");
but i noticed that every time it is clicked the activity keeps stacking up and i end up having to use the back button a bunch of times to get out of it. I have always used android:noHistory="true" in the manifest but obviously i cant do that here so is there a way to do the same thing with when the intent is launched?
Try using arg1.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) before calling your intent, which might solve your problem...
You can use Intent.FLAG_ACTIVITY_REORDER_TO_FRONT. If the activity is already running, it'll be brought to the front.