App restarts rather than resumes
Aha! (tldr; See the statements in bold at the bottom)
I've found the problem... I think.
So, I'll start off with a supposition. When you press the launcher, it either starts the default Activity
or, if a Task
started by a previous launch is open, it brings it to the front. Put another way - If at any stage in your navigation you create a new Task
and finish
the old one, the launcher will now no longer resume your app.
If that supposition is true, I'm pretty sure that should be a bug, given that each Task
is in the same process and is just as valid a resume candidate as the first one created?
My problem then, was fixed by removing these flags from a couple of Intents
:
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
While it's quite obvious the FLAG_ACTIVITY_NEW_TASK
creates a new Task
, I didn't appreciate that the above supposition was in effect. I did consider this a culprit and removed it to test and I was still having a problem so I dismissed it. However, I still had the below conditions:
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
My splash screen was starting the "main" Activity
in my app using the above flag. Afterall, If I had "restart" my app and the Activity
was still running, I would much rather preserve it's state information.
You'll notice in the documentation it makes no mention of starting a new Task
:
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.For example, consider a task consisting of the activities: A, B, C, D.
If D calls startActivity() with an Intent that resolves to the
component of activity B, then C and D will be finished and B receive
the given Intent, resulting in the stack now being: A, B.The currently running instance of activity B in the above example will
either receive the new intent you are starting here in its
onNewIntent() method, or be itself finished and restarted with the new
intent. If it has declared its launch mode to be "multiple" (the
default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same
intent, then it will be finished and re-created; for all other launch
modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be
delivered to the current instance's onNewIntent().This launch mode can also be used to good effect in conjunction with
FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task,
it will bring any currently running instance of that task to the
foreground, and then clear it to its root state. This is especially
useful, for example, when launching an activity from the notification
manager.
So, I had the situation as described below:
A
launchedB
withFLAG_ACTIVITY_CLEAR_TOP
,A
finishes.B
wishes to restart a service so sends the user toA
which has the service restart logic
and UI (No flags).A
launchesB
with FLAG_ACTIVITY_CLEAR_TOP,A
finishes.
At this stage the second FLAG_ACTIVITY_CLEAR_TOP
flag is restarting B
which is in the task stack. I'm assuming this must destroy the Task
and start a new one, causing my problem, which is a very difficult situation to spot if you ask me!
So, if all of my supposition are correct:
- The
Launcher
only resumes the initially created Task FLAG_ACTIVITY_CLEAR_TOP
will, if it restarts the only remainingActivity
, also recreate a newTask
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()
ionic 3 App restarts rather than resumes
I have used plugin https://ionicframework.com/docs/native/app-minimize/ to minimize the app
In app.component.ts
import { AppMinimize } from '@ionic-native/app-minimize';
import { App, Platform } from 'ionic-angular';
constructor(platform: Platform,public appMinimize: AppMinimize, public app: App){
platform.ready().then(() => {
platform.registerBackButtonAction(() => {
let nav = app.getActiveNavs()[0];
let activeView = nav.getActive();
if (activeView.name === "Page1") {
/** minimize the app in background **/
this.appMinimize.minimize();
}
else {
nav.pop();
}
});
});
}
Windows Phone App Restarts instead of Resumes although it Appears in Task List
I was told by Microsoft:
The OS can terminate an app in order to free up resources. But such an up can still appear in the task bar.
If the suspended app was terminated, there is no Resuming event and instead OnLaunched() is called with an ApplicationExecutionState of Terminated. So, the routines to restore the app state must be called within OnLaunched().
Sometimes it's required to distinguish if the app has been relaunched by tasklist or by tile. For this, use the TileId property in the OnLaunched(). TileId is "App" if the app has been started by tile and it is empty if the app has been relaunched by tasklist. TileId could also have another value if you generate eg. a second tile for the app giving them another id.
However, this does not work in Windows 8.1 as the value of TileId is always "App" independent of starting it by tasklist or standard tile.
app restarts after exiting the app using system.exit
Can You try my code, I also used dialog box for exiting my app and it's work fine for me. Hope it will do same for you
((Activity) context).finishAffinity();
System.exit(0);
Related Topics
How to Center the Elements in Constraintlayout
How to Quit Android Application Programmatically
Android Volley Basicnetwork.Performrequest() Unexpected Response Code 404
How to Execute Adb Commands Through My Android App
How to Select Only One Checkbox in Recyclerview and Notifydataset Changed
How to Open Whatsapp Using an Intent in Your Android App
Android Webview & Localstorage
You Need to Use a Theme.Appcompat Theme (Or Descendant) With This Activity
Android Volley - Basicnetwork.Performrequest: Unexpected Response Code 400
Limitations on Opening Pdf File in Android
Retrofit 2 Json Response Json Array and Object
How to Show a Video Thumbnail from a Video Path
Changing Background Color of the Layout on a Button Click in Android