App always starts fresh from root activity instead of resuming background state (Known Bug)
This is due to the intents being used to start the app being different. Eclipse starts an app using an intent with no action and no category. The Launcher starts an app using an intent with android.intent.action.MAIN action and android.intent.category.LAUNCHER category. The installer starts an app with the android.intent.action.MAIN action and no category.
Whoever submitted the bug should have probably worded it as a request for enhancement to the Eclipse plugin since they apparently want Eclipse to have the ability to pretend to be the launcher and to start apps using the same intent as the launcher.
App restarts main activity from background instead of resuming previous state
I actually found why finish()
crashed the app: onDestroy()
was called and it tried to unregister a receiver that was not registered yet, so the app crashed: Unable to destroy activity [...] Receiver not registered
.
So that code actually works fine for my issue:
if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
I just didn't pay attention to onDestroy()
Android: When resuming app after pressing home, app always starts at root Activity
Android will destroy Activities
if the system needs the memory. You have to make sure that you save the state of your Activity in onSaveInstanceState()
so that you can manually resume it afterwards. You can save the needed values in SharedPreferences
or in an SQLiteDatabase
Finished Activity starting up on resuming the app from background
Whenever you press the back button, Android would assume you want to exit the app. So when you open the app again, it will start from the beginning.
By checking whether or not user has logged in, you can decide to show Activity A or B. Without being able to check your code, it's difficult to say where stops you doing that. One possible solution is that save user login status in SharedPreferences so you can check if user has logged in when the app starts.
How to return to the latest launched activity when re-launching application after pressing HOME?
Oh, I think I've found the answer.
Because I was launching the app by using IntelliJ, it seems it launches the application in a different way than a user, clicking a home screen widget, would launch. It's explained in the answer to another SO question:
This is due to the intents being used to start the app being different. Eclipse starts an app using an intent with no action and no category. The Launcher starts an app using an intent with android.intent.action.MAIN action and android.intent.category.LAUNCHER category. The installer starts an app with the android.intent.action.MAIN action and no category.
Ref: App always starts fresh from root activity instead of resuming background state (Known Bug)
So I've manually killed the application in the phone, and relaunched it again from the home screen widget. Then opened the GameActivity, and pressed HOME. Now, when relaunching it the GameActivity is still visible and keeping its UI state as it was when I left it.
And I guess that the reason why a new instance of the activity was created when pressing the shortcut before was due to a different Intent being used for starting the activity.
Related Topics
Retrofit Uploading Multiple Images to a Single Key
Interstitial Admob Ads: "Illegalstateexception: Only Fullscreen Activities Can Request Orientation"
Assign Width to Half Available Screen Width Declaratively
How to Implement Uncaughtexception Android
How to Create Expandable Listview in Flutter
Android Webview with Client Certificate
Add Onclick Listener to Predefined Button
Admob Getting an Ad Response. Errorcode: 0 Failed to Load Ad:0
Android - Preserve or Delete Files Created by the Application on Uninstall
Libgdx Spritebatch Render to Texture
How to Use Isineditmode() to See Layout with Custom View in the Editor
Displaying Yuv Image in Android
How to Make an Intent with Multiple Actions
How to Change the Android App Package Name When Assembling with Gradle
How to Write Jarlist Cache File While Creating Android Project