In my Application, activity A is launcher activity, from A it called B and from B it called C, I have such more than 5 activities. In C when I press home button, and again open my app it open C, that is fine in my case. But after pressing home button in C, when it idle for some time and application is killed, after that when I open my app it opens C. But I want to open main launcher activity that time. How can I do this?
A > B > C > HOME button > idle for some time > application killed > open app > C.
In this case I want to open main activity A instead of C.
When Home button is pressed, onStop method is called in your activity. So what you may do is to add finish(); in onStop method to destroy your activity. Eventually onDestroy method will be raised to confirm that your activity is finished.
Pressing the Home switches you from the app to the home screen, whilst leaving your app running in the background.
In android, by pressing a back button or home button. So put an event key listener for back & home button and terminate the service.
ViewModels are automatically destroyed by the system when your user backs out of your activity or fragment or if you call finish() , which means the state will be cleared as the user expects in these scenarios. Unlike saved instance state, ViewModels are destroyed during a system-initiated process death.
You will need to detect that Android has killed your process and then after that the user has returned to the app (causing Android to create a new process). I've described how to do this in numerous answers:
https://stackoverflow.com/a/29802601/769265
https://stackoverflow.com/a/27276077/769265
https://stackoverflow.com/a/11249090/769265
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With