Android Question Killing previous instance (main) with GCM

Luiz Fernando Orlandini

Active Member
Licensed User
Longtime User
Hi All.

I getting the error killing previous instance (main), and I don't have any idea about how to solve it.

My scenario is below:

I have my app implemented in the Main class and I have a second class called NewOrder, that is used to receive and process the notifications.

My App receives many notifications directly from GCM implementations.

When the message arrived, I create a new instance of NewOrder using, StartActivity(NewOrder).

Everythings works fine until here. But when I decide to do another tasks in the mobile phone, call somebody and even turn off the device, when I got back to the application, when it starts I receive a white screen, like if the application has lost the flow and in the log I can see the message Killing previous instance (main).

I have an option to pull the back button also, and when I do that, I came back to the correct NewOrder screen, and when I finalize the process, the application just closes.

Maybe an alternative, is put all the code of NewOrder, in the Main class, but I believe, that I can correct something in my code that will correct the behavior of my APP.

Regards.
 

Luiz Fernando Orlandini

Active Member
Licensed User
Longtime User
Hi Erel.

Follow the code of Main class.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    If (FirstTime = True) Then
        Frame = 0
        TimerGif.load(File.DirAssets, "waiting.gif")
        TimerForGif.Interval = TimerGif.Delay(Frame)
        TimerForGif.Initialize("TimerForGif", TimerForGif.Interval)
        TimerForGif.Enabled = False
               
        Dim PhoneInfo As PhoneId
        If PhoneInfo.GetSimSerialNumber.Trim.Length > 0 Then
            SimCardNumber = PhoneInfo.GetSimSerialNumber
        Else If PhoneInfo.GetSubscriberId.Trim.Length > 0 Then
            SimCardNumber = PhoneInfo.GetSubscriberId
        Else If PhoneInfo.GetLine1Number.Trim.Length > 0 Then
            SimCardNumber = PhoneInfo.GetLine1Number
        Else
            SimCardNumber = PhoneInfo.GetDeviceId
        End If
       
        If (File.Exists(File.DirInternal, "PrefTaxiSP.txt") = True) Then
            ScreenToLoadWhenBack = "FirstMenu"
            Activity.LoadLayout("Login")
            loadEditLogin
           
            chkAutomaticLogin.Checked = True
            Dim Login As LoginPreferences
            Login.Initialize("", "")
            Dim m As Map = Login.GetPreferences
            edtPhoneNumber.Text = m.GetKeyAt(0)
            edtPassword.Text = m.GetValueAt(0)
        Else
            Activity.LoadLayout("FirstMenu")           
        End If

        UserLogged = False
        UserAvailable = False
               
        MyFade.Initialize(Activity, Me, "powered-splash.png", Gravity.CENTER_VERTICAL, 5000, 1000, "Fade", False)
    Else
        If StateManager.RestoreState(Activity, "Main", 60) = False Then
            'set the default values
            If (UserLogged = False AND ScreenToLoadWhenBack <> "FirstMenu") Then
                ScreenToLoadWhenBack = "FirstMenu"
                Activity.Invalidate
                Activity.RemoveAllViews
                Activity.LoadLayout("Login")
                loadEditLogin

                If (File.Exists(File.DirInternal, "PrefTaxiSP.txt") = True) Then
                    chkAutomaticLogin.Checked = True
                    Dim Login As LoginPreferences
                    Login.Initialize("", "")
                    Dim m As Map = Login.GetPreferences
                    edtPhoneNumber.Text = m.GetKeyAt(0)
                    edtPassword.Text = m.GetValueAt(0)
                End If
            Else
                If (ScreenToLoadWhenBack <> "NewOrder" OR _
                    ScreenToLoadWhenBack <> "OrderAccepted" OR _
                    ScreenToLoadWhenBack <> "FinalizeOrder" OR _
                    ScreenToLoadWhenBack <> "WaitingCustomer") Then
                   
                    Activity.Invalidate
                    Activity.RemoveAllViews
                End If
               
                If (Activity.RequestFocus = False) Then
                    Activity.LoadLayout(ScreenToLoadWhenBack)
                End If
            End If
        End If   
    End If
End Sub


From NewOrder class.

B4X:
Sub Activity_Create(FirstTime As Boolean)   
    If (FirstTime = True) Then
        Frame = 0
        TimerGif.Load(File.DirAssets, "waiting.gif")
        TimerForGif.Interval = TimerGif.Delay(Frame)
   
        If (TimerForGif.IsInitialized = False) Then
            TimerForGif.Initialize("TimerForGif", TimerForGif.Interval)
        End If
    End If
End Sub
 
Upvote 0

Luiz Fernando Orlandini

Active Member
Licensed User
Longtime User
One more relevant information.

When I receive the GCM message in the Service Module it is executed...

B4X:
Sub MessageArrived (Intent As Intent)
    Log("New notification arrived")

    If (Main.UserAvailable = True) Then    
        StartActivity(NewOrder)
            
        CallSubDelayed2(NewOrder, "NewNotificationArrived", Intent)
    Else
        Log("Inactive user in system")
    End If
End Sub
 
Last edited:
Upvote 0

Luiz Fernando Orlandini

Active Member
Licensed User
Longtime User
I don't completely understand the code above. However you should make sure that the layout is always loaded in Activity_Create. Even if FirstTime is False.

Erel, but why the Main class has be killed? Once the class was killed, any code of Main Activity_Create will be executed, Am I rigth?
 
Upvote 0

Luiz Fernando Orlandini

Active Member
Licensed User
Longtime User
I resolved this behavior using Service.StartForegroud
It was necessary to join my two activities in just one, cause the notification was direction to the wrong one.
 
Upvote 0
Top