I'm looking to find out how to stop an activity instead of resuming upon the click of the item on the notification list. Any ideas?
Overload onNewIntent in your activity and when you get the intent from the pending intent just call finish()
Your activiy will need to be a singleInstance activity though. Otherwise, a new activity will get created on the task stack when the notification item is clicked.
Can you be more specific about what you're doing. Are you trying to write another Task-Killer-like application? or perhaps some kind of music player application? Telling us about the kind of application you're writing can help us help you.
By the way, using the term "resuming" shows that you haven't fully understood the Activity lifecycle yet. onResume is for resuming the UI thread. onRestart is the one that's used for restarting the Activity.
Related
To avoid OnDestory() and OnCreate() beeing called all the time when navigating between my main activity and some sub activities, I have set the singleTop option in my manifest. Unfortunatly this causes that the UI in my main activity is gone when returning from a sub activity. Do I really need to redraw my UI manually?
I am still wondering how to handle these basic navigation features. Is it unusual to perform application initialization tasks (e.g. start services) in OnCreate() of the main activity?
I must be missing something from your explanation I guess because the very basic point that you provided is not correct. When you launch another (sub) activity from your activity then your starting activity is not destroyed but it is stopped. So your onStart is called when you navigate back.
So maybe you can explain a little bit more the background of your requirements.
Use android:launchMode="singleTask" for your main activity so the system will not re-create it if you call it via Intent. You can also clear the stack and go back to your main activity by using
Intent intent = new Intent(SubActivity.this, MainActivity.class);
intentHome.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentHome.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intentHome);
I want my activity the recognize when it is left and a new activity is started so for example when i'll do
startActivity(intent);
It will perform a certain code.
I tried using onPause();
But it only work on leaving the activity manually
Why don't you perform the operation before calling the new startActivity() and go read about the android activity life cycle to understand how it works
EDIT::
if i truly understand what you are saying..then i think you are looking for onStop() method
Look at the Android activity lifecycle here. onPause is called when "another activity comes to the foreground". If I understand your question correctly, it seems like overriding your first activity's onPause method will work. It's called when you add a new activity on the back stack or when you send the app to the background.
For convenience, here's the chart from the link I provided that I refer to for activity lifecycle questions:
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.
I have two Activities in my app, a "Main" and a "Prompt" Activity. The Prompt is called from a button on the ActionBar in the Main Activity, and the Prompt has several EditText objects and a Spinner. The Prompt also has a Button that, when pressed, should validate the values in these objects and then send them back to the Main Activity.
I know you can send this information as a Bundle by placing it in an Intent and calling StartActivity. However, if I understand the Activity lifecycle, doing a startActivity() call to start the Main Activity will keep the old version of Main on the backstack and take up unneeded resources, as well as make it possible for the user to "clear" back to Prompt and then to the old Main Activity as well.
I want both the old Main activity as well as the Prompt activity to be removed from the backstack while also sending the values from Prompt's fields to a new Main activity. Can anyone help me figure out the most ideal way to accomplish this?
Thank you!
The best way to do this would be to start Prompt with startActivityForResult() then return the values to onActivityResult()
Docs with example
If for some reason this won't work for you then you can probably find an Intent flag that will accomplish what you need depending on your situation. One that may work for you is FLAG_ACTIVITY_REORDER_TO_FRONT This will bring your MainActivity to the top of the stack
Also, as long as you call finish() on your PromptActivity after calling startActivity then it will be cleared from the stack
You should watch Google I/O Navigation. It explains navigation and how the stack works very well
I have this requirement to send my application background and then bring it to foreground on some key capture intents (not from application launcher offcourse) So How can I send the current tasks to background and bring the same to foreground ?
Use moveTaskToBack() to send the activity in the background and still running if the user presses the back key.
see :Activity for the way on how to do this. its quite simple.
so in order to do this you will also need to override the onBackPressed() method or onKeyPressed() and call this method if the back button was pressed (dont forget to return true on the back pressed methods so android is aware that you consumed the event and doesnt finish the activity).
For returning to this activity that you have moved to the background you can post a notification with a pending intent to launch it back and that will automatically bring the activity to foreground.
Hope this helps.
To send you application to background you should call moveTaskToBack() from your Activity class. When your Activity gets new intent (btw. the onNewIntent() method from your Activity will be called) your Activity gets into foreground by system (you don't have to do anything).
What do you mean by "background?" Activities are stacked one upon another as you create new Activities, then accessed in reverse order using the device's back button. Think of the push() and pop() methods, it's the same paradigm. Applications that need to have code running non-interactively should extend android.app.Service, but beware that you can do some real damage implementing a service. Rogue processes can drain battery life and reduce UI responsiveness.
I solve all the problem pertaining to notification start with fresh activity after moveTaskToBack(true) when back key is pressed by
adding to manifest android:launchMode="SingleTask" android:clearTaskOnLaunch="true"in the activity xml markup section