When i kill the running instance and relaunch the application using Db ecncryption app is crashing

When i kill the running instance of application and relaunch the application using Db encryption app is crashing

i have implemented code below scenarios

namespace Inspect.Droid
{

/// <summary>
///Main Activity Class
/// </summary>
[Activity(Label = "Inspect", Icon = "@drawable/icon", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, Theme = "@style/InspectTheme", ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
    public static Context GLOBAL_CONTEXT;
    public static Activity MainAppActivity;
    public static Context ACTIVITY_CONTEXT;
    public static FragmentManager FragmentMgr;
    public static Action<bool, string> passcodeCallback;
    public static int REQUEST_PWD_PROMPT = 101;
    private string dateTimeFormat;
    private DataBaseEncryption dbEncryption;
    //async Task PrepareDb()
    //{
    //    await dbEncryption.InitiateDecryption();
    //}
    /// <summary>
    ///Override OnCreate Event handler 
    /// </summary>
    /// <param name="bundle">Bundle.</param>
    protected override async void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        MainAppActivity = this;
        Forms.Init(this, bundle);
        //to get the current device language code
        var code = Locale.Default.GetDisplayLanguage(Locale.Default);
        CommonInfo.Instance.DeviceCurrentLanguageCode = code.Substring(0, 2).ToUpper();
        Console.WriteLine("Device lang code: " + CommonInfo.Instance.DeviceCurrentLanguageCode);
        try
        {
            ConfigurationManager.Initialise(PCLAppConfig.FileSystemStream.PortableStream.Current);
        }
        catch (Exception ex)
        {
            //when application coming back from background throws exception, bcz PCLAppConfig is already initialized, so as there is no change no need to change here
            Console.WriteLine(ex.Message);
        }
        GLOBAL_CONTEXT = Android.App.Application.Context;
        ACTIVITY_CONTEXT = this;
        FragmentMgr = FragmentManager;
        dbEncryption = new DataBaseEncryption();
        //await PrepareDb();
        await  dbEncryption.InitiateDecryption();
        //AzureMobileServices Initialization for Android
        Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();
        SessionInfo.Instance.DeviceId = Utility.GenerateGuid().ToString();

        //This will set the value for _skipCount variable in all respected helper classes.
        SetSkipCount();
        Inspect.ScreenSize = new Xamarin.Forms.Size(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density,
        Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density);

        Inspect.AndroidDisplayMetricsDensity = Resources.DisplayMetrics.Density;
        int statusBarResId = Resources.GetIdentifier("status_bar_height", "dimen", "android");
        if (statusBarResId > 0)
        {
            Inspect.AndroidStatusBarHeight = Resources.GetDimensionPixelSize(statusBarResId);
        }

        Context context = this.ApplicationContext;
        Acr.UserDialogs.UserDialogs.Init(this);
        SessionInfo.Instance.Version = context.PackageManager.GetPackageInfo(context.PackageName, 0).VersionName;

        //allowing the device to change the screen orientation based on the rotation
        MessagingCenter.Subscribe<InspectionAddNotesPage>(this, CommonConstants.AllowLandScape, sender =>
        {
            RequestedOrientation = ScreenOrientation.Unspecified;
        });
        //during page close setting back to portrait
        MessagingCenter.Subscribe<InspectionAddNotesPage>(this, CommonConstants.PreventLandScape, sender =>
        {
            RequestedOrientation = ScreenOrientation.Portrait;
        });
        LoadApplication(new Inspect());
        //Intializing the context for Media access
        CrossCurrentActivity.Current.Activity = this;
        PasscodeAuthDependency.Activity = this;
    }

    private void SetSkipCount()
    {
        switch (GetDeviceRAMSize())
        {
            case 0: //RAM is ~1 GB
                SessionInfo.Instance.SkipCount = 1000;
                break;
            case 1: //RAM is ~2 GB
                SessionInfo.Instance.SkipCount = 2000;
                break;
            case 2: //RAM is ~3 GB
                SessionInfo.Instance.SkipCount = 3000;
                break;
            case 3: //RAM is ~4 GB
                SessionInfo.Instance.SkipCount = 4000;
                break;
            case 4: //RAM is ~5 GB
                SessionInfo.Instance.SkipCount = 5000;
                break;
            case 5: //RAM is ~6 GB
                SessionInfo.Instance.SkipCount = 6000;
                break;
            default:
                SessionInfo.Instance.SkipCount = 2000;
                break;
        }
    }

    /// <summary>
    /// Override Back button 
    /// </summary>
    public override void OnBackPressed()
    {
        //allowed back press for projectlist page if need to cancel long press highlight 
        if (SessionInfo.Instance.LblBack.IsVisible)
        {
            base.OnBackPressed();
        }
        if (Inspect.Instance.DoBack)
        {
            base.OnBackPressed();
        }
    }
    protected override void OnStop()
    {
        base.OnStop();

        dbEncryption.InitiateEncryption(true);

    }
    protected override void OnRestart()
    {
        base.OnRestart();
        dbEncryption.InitiateDecryption();
        Console.WriteLine("OnRestart<<<<<<<<<<<<<<<<<", "OnRestart");

    }
    protected override void OnDestroy()
    {
        base.OnDestroy();
        Console.WriteLine("OnDestroy<<<<<<<<<<<<<<<<<", "OnDestroy");
    }

Please provide the solution.

Sign In or Register to comment.