In my app I have a main activity that is started when the app is launched from the app drawer. In the app there is another activity B that can be launched from the main activity.
If I press home while in activity B to return to home, Activity B will be started instead of the main activity if I launch my app through the app drawer again.
Is there a way to make only the main activity start even if the user pressed home in activity B without using noHistory?
Any help would be appreciated!
You just don't want to keep state in B, on the Activity B onStop method, just call finish();
I set a separate android:taskAffinity for activity B
Related
I have two activities A & B. in A, i am showing a list of titles, and on clicking a title, it will open the detailed article in activity B.
I declare A as singleinstance in Manifest.
But if I declare A as single instance, and when the Activity B is opened and paused, then Activity A is not available on backstack.
I will try to explain by reproducing:
Activity A (launchMode = SingleInstance) with list of titles.
On clicking a title, Activity B opens
On clicking back button/up navigation, Acitiviy B finishes and Activity A resumes.
Again open activity B.
Press home button of device (Activity B goes to background - onPause)
Activity B is available in Recent Apps
Open app from recent apps/launcer - Activity B opens
Clicking back button/up navigation - Act B finishes, but Act A not resumed.
How can I provide better up navigation?
For my idea, you can change lauchmode of Activity A and Activity B from Singleinstance to android:launchMode="singleTop". I work fine for me. It is well said here. Let try.
When you will press the home button the activity B would call onStop() method. For a better understanding you can refer to https://developer.android.com/guide/components/tasks-and-back-stack.html
I'm following the next flow, Start activity A (loading resources), then go to Activity B (login). I marked Activity A as noHistory = "true" and just after I start the activity B I call finish() on activity A.
The app works as expected and when I pressed the back button from the Activity B the app closes, when I pressed the recents button and select my app, it restarts from activity A, I want it to starts from activity B.
thanks.
Add this to your activity declaration in the AndroidManifest:
android:excludeFromRecents="true"
And it will be excluded from the "recent apps" list
that is expected behaviour . Since you have pressed the back button the app is closed , if you launch from recent after that its like clicking on app icon to launch the app . You can use shared preferences to save if the first activity is already launched you can directly launch second activity
There are two activities in my application Activity A ( Launcher Activity) and activity B. first i launch an application(A activity) . onBackpress(),i am moving A to backstack. I am launching other app and firing intent from there to launch activity B. Now activity A which is in background is getting launched first then B is coming top of it.
How to make activity A to be in backstack and opens only activity B on firing intent from other application?
I have activity stack in my android application.Suppose A and B.When the application starts the Activity A is visible.From this activity i call Activity B using startActivityForResult method.Now from here if i press Home button of device then activity runs in background that is OK.But when i relaunch the application by taping on activity icon a new instance of activity is start and show activity A rather showing the activity B.Please help me in this.Any help would be highly appreciated.
Check out if your launchMode of the Activity A is SingleTask.
You can try calling activity B using getApplication().startActivity(myIntent);
I have a splash activity (A) that calls a listview activity (B) which calls another activity (C).
When I'm on activity C and I press Home, than kill the app (or wait of Android to do it), than longpress Home and come back to activity C there's a strange problem:
When I click back I go back to B. Than I have a backbutton handler that asks the user if they want to exit and calls finish() on the activity. When I try to exit in this scenario, activity A starts again.
On regular operation it finishes B and doesn't go back to A.
Why is that??
Thanks
When the app is killed (either by you or by Android) the process hosting your applications is killed. However, Android remembers the state of the activity stack (in your case A->B->C).
When the user returns to the app, Android creates a new process for the app and recreates only the activity that was at the top of the activity stack (in this case: C). Now the user presses BACK, which causes activity C to finish and Android recreates the instance of activity B which is then shown (You will see calls to B.onCreate(), B.onStart() and B.onResume()).
Now the user presses BACK again. Your back button handler tries to call finish() on activity A, but there is no instance of activity A. Android hasn't created it yet! When activity B finishes Android remembers that there was an instance of activity A in the activity stack underneath B so it recreates the instance of activity A which is then shown (You will see calls to A.onCreate(), A.onStart() and A.onResume()).
I hope this explains what you are seeing.
Make sure you are calling finish() on A when you load B