I have an Android activity which checks if the user's GPS is turned on and gives the choice to go Settings to turn it on if it's off. However, when I return from Settings and pop my activity from back stack, I want onCreate() to run again and execute code which uses my location. I have tried running recreate() in the onResume() method, however in that case it will not load my activity at all.
Any suggestions on how to execute this task are welcome. Thank you.
when you moved from activity to settings then, Activity will go to onStop(). So, you can call onRestart() method in onStop() method will resume your Activity. Hope this option will help.
Or if this link is your case then u can find answer on same page.
How can I return back to my Activity from GPS settings window
Related
I have an Activity A which is listening to some websocket message, when it receive certain message, it will navigate to Activity B by startActivity().
Things works fine when App is on foreground, but when it's on background, the startActivity() will cause the App bring to front automatically.
How can I "sliently" startActivity() in background without bringing
the app to foreground?
You can't. What you can do is set a flag, then in onResume call startActivity for the next activity if the flag is set.
Or use fragments instead of activities.
It is not directly possible to do so, However you can try something like minimizing the app as soon as the activity gets started.
In the onCreate method of your activity use the following statement:
YourActivity.this.moveTaskToBack(true);
Of course you will need to use some flags and extras to prevent it from being minimized in other cases too.
as #Gabe said, You can do this by setting a flag, same as we do open login or main activity by checking user object in SharedPreference.
if(SharedPreference Message count > 0){
startActivity(new Intent(this, YourChattingActivity.class));
}
Also you will need to set message count in SharedPreference.
Using below link you are able to achieve what you want:
Here is the solution to check activity status and handle according to that.
I have some kind of a login activity which returns to main if login successfull. But after that, if user presses hardware "back" button, main activity is restarting for some reason...
It seems like it is going to previous (login) activity, but since it has already finished, parent activity is restarted.
I have tried using <activity android:noHistory="true"> on login activity in manifest. startActivityForResult() is not a solution for me as well. Problem remains.
Can you help me with any other ideas?
UPD: I am calling finish() when going back to main activity
This is probably because you haven't finished your activity when you're 'returning' to main. Check where you create your intents, always finish activities you do not want to be accessed via the back stack.
try use this after each startActivity()
startActivity(intent);//you already have this
finish();//add just this .. and report
Do not call finish() inside of onBackPressed() method. Android / OS will do it for you automatically.
Call finish() only If login was ok and you want to close the login activity. Of course only if main activity present in the back stack.
If back stack contains no main activity, so open it by using of common startActivity method. Also check this answer to remove an activity from the back stack.
Problem is solved by forcely exiting the whole application in main activity's onBackPressed() method. Although, it doesn't seem like a clear solution to me.
So I've looked up similar problems to this, and have followed the advice in those threads, but it seems to give me no change in behavior.
So I'm writing an app that essentially notifies the user when they're going too fast or too slow based on GPS onLocationChanged() calls. I overrided the onBackPressed() method to finish() and return but the activity continues to run in the background when I go back to the main activity.
To be clear, I DO want the activity to run when the app is minimized or screen is off. I only want it to stop when the user goes back to the menu (IE, hits the back button)
Here's the link to the class on pastebin:
http://pastebin.com/V7z5c3HH
Thanks for your help! =D
Unsubscribe your location listener in the onDestroy method.
However, what you need for your GPS processing is probably a Service, not an Activity.
You need to remove the updates for that listener.
#Override
protected void onDestroy() {
locationManager.removeUpdates(locationListener);
super.onDestroy();
}
I have some floating bugs in my app, which unable to reproduce clearly. I suspect them from inproper work of my SaveInstanceState|restoreInstanceState mechanism, so I need to check case, when activity is being stopped when goes to background, and recreating after I press back button from spawned activity. Is there a way to force android stop and destroy activity which went to background? It should remain on activity stack, so I cannot just finish it.
Just enable the developer option "don't keep activities" (or whatever its called). This won't remove the activity from the stack, but will actually call onStop() and onDestroy() whenever the user leaves the activity and opens another. When the user presses BACK, Android will create a new instance of the activity, and call onCreate() and onRestoreInstanceState()` as expected.
Looks like it's a copy of this and this questions. Override onPause() method and call method finish() in it.
I have a main activity which calls a child one via
Intent I = new Intent(this, Child.class);
startActivityForResult(I, 0);
But as soon as Child becomes visible the main activity gets its onStop and immediately after that onDestroy method triggered. And as soon as I call finish() within the Child activity or press the back button, the Child activity closes and the home screen shows (instead of the main activity).
How can I prevent the main activity from being destroyed? :\
If you launch a child Activity from which you expect return data, you'll probably want to use startActivityforResult instead.
You may want to check this question: Child Activity in Android as it seems to be the same problem.
Edit:
As for what's happening here, you could place code in the onStop() and/or onDestroy() methods to investigate - at least a call to isFinishing() to check why the Activity is being destroyed.
You should also use adb logcat from your host machine to check the logcat in case it holds more information - and maybe use Log.d() (the result goes into logcat as well) instead of toasts to make sure you don't miss them.
I used Dialog instead of an Activity and everything worked well so I'm leaving it like that.
check androidmanifest nohistory=true and that made the OS destroy the activity before the result. that might be one of the reason for your problem.