Prevent going to parent activity if child has finished - java

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.

Related

How to back to last app from recent list without finishing activity

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?

Switch Activity sliently while app is in background

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.

Is there a way to tell android force stop activity which going to background without removing it from activity stack?

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.

Android avoid OnResume

I have 3 Acivity based application, It's work flow like this MainActivity ListView and DetailView. when onResume event trigger , need to call MainActivity. without going to other two activity.
Is there any way to call MainActivity when onResume event trigger?
Thank You
You can set the android:clearTaskOnLaunch="true" attribute for you MainActivity in the AndroidManifest.xml file. See here to find why and more details. I think this is the most convenient way to meet your demand.
Edit:
I just tested and found this only works when you exit the app and launch the app from the app drawer(NOT long press on HOME and select the app).
If you want to always bring the root activity to the front, no matter when you re-launch the app or from the recent screen. You can declare "android:launchMode="singleTask" for the root activity, here, the MainActivity.
The best solution I can think of is to start the activity again in the onResume of all your other activities:
#Override
public void onResume() {
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
}
The user will still be able to hit the back button and go back to the previous activity, however.
If you want to quit your List/Details views when the user closes your app, have them finish() themselves in their onPause which is called when your Activity is closed.
The only caveat here is that calling finish() will move it one Activity back in the ActivityStack so if your MainActivity isn't the one launching the List/Details views, it will not go back to the MainActivity. In this case, you could specify in the AndroidManifest.xml
<activity android:name="sample.activity.MyActivity" android:noHistory="true" />
to prevent the List/Details activities from ever being put into the history.

Prevent an Activity from being killed by the OS while starting a child activity

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.

Categories