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.
Related
I'm working on an application that connects to a server on a computer, it's about sharing a clipboard, links, etc. I added the activity responsible for sending to the sharing menu so you can send links directly from the browser and here is the problem.
When user select my app I want the activity to be created, sent a link and returned to the browser but without finishing activity.
I want to achieve an effect similar to open recent apps list and select previous app but programmatically.
I tried finish() and onBackPressed() but they close the activity
Finish() will close the activity, as it is it's role. But onBackPressed() should just call onPause. If the activity is killed by onBackPressed() (you should check if onDestroy() is actually called by, for example, overriding it and printing something), then it's possible that you are assigning a flag during the creation of activity that forces it to close later. Can you provide the code that shows the part where you create your activity?
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.
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 am making a game for Android, but I never really found a good way to change the activities, or the content views. I have 1 main menu activity now, whose content view receives MotionEvents and dispatches them to my custom buttons. Another Activity has a simple contentView which just paints the screen in one color. All contentViews have the same base class and activity 2 is derived from activity 1.
The problem is, that the app just crashes if a try to change the activity. It takes about 20 seconds, then the error message appears that says the app isnt responding.
In logcat, theres also a message keyDispatchingTimedOut sending to activity2
Below is the code for activity change:
public void changeActivity() {
Log.d("changing", "activity");
Intent i=new Intent(this, Activity_Level.class);
startActivity(i);
}
Any ideas?
You have to use context of your activity in method
Intent i=new Intent(Youar_Activity_Name.this, Activity_Level.class);
^^^^^^^^^^^^^^^^^^^^^^
Use above code in your changeActivity() method.
if the app isn't responding , it's because you do a long operation on the UI thread . maybe after calling this function you continue to do something else ?
if , as people said , the activity isn't opened (and you can check it by writing to the log inside the onCreate method) , check the manifest.
in any case , if you want to have better control of activities , you can check the possible flags to use for the intents , and you can also use fragments (when possible) , just like google recommends .
Without seeing the logcat message, I bet you forgot to add the activity in AndroidManifest.xml
I sense that you didn't add all the activities to the manifest.xml file. Try to add all the activities there, and give it a run.
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.