I am using the following code to close the android app in a Xamarin Form project.
var activity = (Activity)Forms.Context;
activity.FinishAndRemoveTask();
It is closing the app but if again I tap on the app it is not opening the new instance of the app as the I can see debugger is still active .
Can anyone help ?
it is not opening the new instance of the app as the I can see debugger is still active
When you use activity.FinishAndRemoveTask() method, this method cant kill your Android app, it just means :
Finishes all activities in this task and removes it from the recent tasks list.
You could use the following code to implement this feature :
FinishAndRemoveTask();
Java.Lang.JavaSystem.Exit(0);// Terminate JVM
Related
Is it possible to attach the Android debugger of Android Studio again in order to check Break point after app has been closed ?
You may be able to attach a debugger if the app process is invisible but still alive. If a process is terminated or not is up to the Android OS.
However you will not be able to "check" a breakpoint as breakpoint only work if the code the breakpoint is contained is executed. Breakpoints therefore only work interactively - if you attach to a process after a breakpoint has been passed it is impossible to extract information regarding the process state (variables, ..) at the time the breakpoint was passed, because it is no longer available.
You are probably trying to open the app from a notification or service when your app is closed or has been removed from the background by the Android system.
Here is a nice workaround for that.
Make a new class named Notification.class and extend Application class to it as shown below.
package com.example.app;
import android.app.Application;
public class Notification extends Application {
#Override
public void onCreate() {
super.onCreate();
}
}
After this, in your AndroidManifest.xml file, make the following changes.
<application
android:name=".NotificationClass"
.
.
android:usesCleartextTraffic="true">
</application>
After this whenever you receive a notification for your app and your app is not in background or foreground, you will be able to attach the debugger from Android Studio before clicking on the notification.
Hope it helps. :)
When I am using BackHandler.exitApp() to close the app and not removing the app from task manager window of the android, when I am again opening the app, it seems before it starts the service init, it start to call service functions.
So as a result, I am doing init of this.db = new Datastore({ filename: dbPath, autoload: true, ...options }); , But before it happens the database service is already trying to use this.db, so, as a result, it is saying this.db as undefined.
Can anyone tell me how can I do the init first and then start the services everytime I open the app? Or how can I exit a react native app with completely kill all the tasks?
I solved this by this module react-native-exit-app - https://www.npmjs.com/package/react-native-exit-app
It closes the app completely.
But it would be great if someone can explain the question.
We are trying to prevent the app to restart on exception log the error and kill the app gracefully.
We use the AndroidEnvironment.UnhandledExceptionRaiser += HandleAndroidException; in the mainLauncher / activity to catch unhandled exceptions.
Problems are:
In the HandleAndroidException method, we wish to display a notification to the user letting know the application will close.
The activity object is not available so we are unable to show a HUD / dialog.
After showing the dialog we wish to kill the app and prevent it to restart in a unusable state. ( have tested this.FinishAffinity(); ,Finish(); System.Diagnostics.Process.GetCurrentProcess().Kill(); etc.)
The app still restarts.
In the same method we try to track the error in Visual Studio App Center
(Crashes.TrackError(e.Exception);) The exception is not logged in the appcenter.
(Writing the error to a local logfile works fine.)
In android 4.4 and below, who loads a native application (/system/bin/*) at startup, I think that the file init.rc is responsible, it is correct?
Then if a native application crashes (for example /system/bin/mediaserver) it restart automatically, then the question is: who is responsible for the application restart? there is a file?
ActivityManagerService restarts the native apps.
There's usually some chatter in logcat when an app is restarted by the activity manager service, in the normal log and/or event log
(logcat -b events).
More Info:
If you see the code of ActivityManagerService.finishForceStopPackageLocked() method, this method fires an Intent with action Intent.ACTION_PACKAGE_RESTARTED.
And it is called from various methods like:
ActivityManagerService.forceStopPackage()
IPackageDataObserveronRemoveCompleted.onRemoveCompleted()
So internally there's an PackageDataObserver implemented in ActivityManagerService, which observes if any package is removed, and if it's need to be restarted, an intent is fired with the action Intent.ACTION_PACKAGE_RESTARTED
And every package is forced closed using ActivityManagerService.forceStopPackage(), it knows which package to restart.
Hope it clears the doubt.
I have a service which handles my push notification. When the app is running, everything is fine, but once the app is not running I am getting a null reference exception. I have traced it back to the following line :
newIntent = Mvx.Resolve<IMvxAndroidViewModelRequestTranslator> ().GetIntentFor (request);
This line is used to get the intent for the status bar notification so when the user clicked on it it will take him to the appropriate page inside the app. I am pretty sure that This is null because the application is not running and the MvvmCross framework did not have a chance to initialize and register the IMvxAndroidViewModelRequestTranslator.
My question is what should I use as an alternative?
UPDATE
So I dis some reading and I believe that a regular intent wont work and break my app. I was thinking about creating a dummy activity In my app which will get the regular intent I will send and move on to use the IMvxAndroidViewModelRequestTranslator (which should be not null since the app was started) and create an MvvmCross navigation request.
Any thoughts?
UPDATE 2
Tried the approach above with no luck... still failing on the same line...
So basically the question is how can I launch the app from the status bar while making the MvvmCross eco system start as well...
Thanks
Amit
So what I ended up doing is once the notification arrived and the Mvx.Resolve threw an exception I (meaning the app is not running), I saved the notification data to the app preferences and launched the app to its main activity and there I simpley checked for the notification data and if it existed I navigated to the appropriate activity.
When the Android UI starts, then MvvmCross runs Setup to initialise things like IoC, your application, etc
If you need to initialise your MvvmCross application within a non-UI setting, then try the answer from MvvmCross DataService in an Android Broadcast listener which shows how to access the same setup that the UI uses.