Android Question Different behavior with many activities on JB vs ICS

incendio

Well-Known Member
Licensed User
Longtime User
Hi guys,

I created a login form with something like these
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        Activity.LoadLayout("login")
    End If

End Sub

On Layout Login, there is a panel, name PnlLogin, with will be removed upon successful username and password
B4X:
Sub LoginBtn_Click
    PnlLogin.RemoveView
    LoginOk = True
    ReDrawMainLayout
End Sub

Codes for Activity_Resume
B4X:
Sub Activity_Resume
    Msgbox("Resume","")
    If(LoginOk = True) Then
        Msgbox("1","")
        ReDrawMainLayout
    End If
End Sub

And here is the ReDrawMainLayout codes
B4X:
Sub ReDrawMainLayout
    Activity.LoadLayout("main")
    ac2.Initialize("AC", ac2.VERTICAL)
     IsAcInitialized = True

     For i = 1 To 3
       Dim ai As AHActionItem
       Dim bd As BitmapDrawable
       Dim Filename, Text As String
     
       Select i
         Case 1       
           Filename = "Sales.png"
           Text = "Sales"
         Case 2
           Filename = "Browse.png"
           Text = "Browse"
         Case 3
           Filename = "Report.png"
           Text = "Reports"
       End Select

       bd.Initialize(LoadBitmap(File.DirAssets, Filename))
       ai.Initialize(i, Text, bd)
       ai.Selected = False

       ac2.addActionItem(ai)
     Next

End Sub

Here is the codes for menu item click
B4X:
Sub AC_Click (Position As Int, ActionItemID As Int)
    Dim Action As AHActionItem
    Action = ac2.getActionItem(Position)
   
    If(Action.Title = "Sales") Then
        StartActivity(Sales)
    Else If(Action.Title = "Report") Then
        StartActivity(Reports)
    Else If(Action.Title = "Browse") Then
        StartActivity(Browse)
    Else
        ToastMessageShow(Action.Title & " pressed", False)
    End If
End Sub

On Jellybean 4.2.2, app runs ok, but not with ICS.

On ICS, after start other activity, for ex, Sales, when user pressed Back Key, activity return to Main activity, Sub Activity_resume called (MsgBox Resume shows up), but Sub ReDrawMainLayout in that sub never called (MsgBox didn't show), so I got just a blank screen.
This is not the case with JB, Sub Activity_resume called and also Sub ReDrawMainLayout called.

Why is the result is different in ICS vs Jellybean?
Is there a way so that my codes work on all Android ver? Or how to better design login form?

Thanks in advance.
 

incendio

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then

    End If
    Activity.LoadLayout("login")

End Sub
With that code, on ICS, after running another activity, and when back key pressed on that activity, it return to Login Layout, while I want to return to main layout.
That's is why, I put it in between FirstTime.

Anyway, still confused why on JB, Activity resume called
ReDrawMainLayout correctly, while on ICS it was not called at all.
 
Last edited:
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
I can not call Activity.Finish.

Layout Login and Layout Main is in the same activity, which is main activity.
If I call Activity.Finish, I think, it will quit the app.

First, I call Layout Login in the main activity, and if user enter correct data, remove Layout Login with PnlLogin.RemoveView, then call Layout Main.

That is why, I don't understand why JB and ICS gave different result. I never destroyed main activity.
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
My project is about 4MB, so I made a sample.

On this sample, ICS still got a problem, on JB, no program at all.
 

Attachments

  • sample.zip
    359.5 KB · Views: 191
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code is wrong:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   If(FirstTime) Then
     Activity.LoadLayout("Login")
   End If
   

End Sub

Sub Activity_Resume

End Sub

The activity will be blank once the activity is destroyed. Open your app and press on the back key. Now open it again. The activity will be blank.

See this tutorial: Android Process and activities life cycle
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
I am so sorry, there are a missing codes from those sample.
Here are the remaining codes
B4X:
Sub Globals
    Private LoginOk=false As Boolean
End Sub

Sub Activity_Resume
    Msgbox("Resume","")
    If(LoginOk = True) Then
        Msgbox("1","")
        Activity.LoadLayout("main")
    End If
End Sub

'also on Sub LoginBtn_Click, add this line
LoginOk = true

Please add those codes and take a look again.

Take a look at Sub Activity_Resume , on JB, the second MsgBox shows up, on ICS, never shows up.

If I change variable declaration of LoginOk into this
B4X:
[CODE]Sub Process_Globals
    Private LoginOk=false As Boolean
End Sub

ICS can read LoginOk value correctly, and the second message inside Activity_Resume shows up, calling again Activity.LoadLayout("main"), but still not all views redraw.

To handle blank screen, I add this
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'return true if you want to consume the event
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        ExitApplication
    End If
End Sub

I think there is a different on JB vs ICS when handling variables values on many activities.
Is this because B4Android or OS?
 
Last edited:
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
I call ExitApplication because it will completely remove the program from memory. Pressing Back Key will make the program still running on a cache memory. I read that ExitApplication is not advisable, could you tell me why?

Calling Activity_Resume is a last resort, because faced a problem when redrawing the activity, that is why I used variable to control it so activity won't added multiple times.

But on ICS, without declare it in Process_Global, this variable seems destroyed/reinitialize, although activity not destroyed.

Tell me, is this a limitation in B4A? or on OS? I need to know, so perhaps can find another way to make my app runs on all OS.
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
I have isolated the problem.

The problem is, when start another activity from main activity, ICS doesn't not hold variable value in main activity if this variable is not declared in Process_Global.

Attach is a very simple program to show this anomaly.

There are a 3 buttons on the app.
Button1 and Button2 adding a variable value before call another activity.
Button3 shows variable value after activity return to main activity.

Hope you could find the solution, to this anomaly.
Thanks.

Btw, I still use MsgBox to trace the program, cause when using Log, I didn't see anything in Log Tab on B4A IDE.
 

Attachments

  • err.zip
    8.3 KB · Views: 199
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
I am talking variable value on the same activity, not on different activity.

On JB, variable value retain even it is not declared in Process_Global.
On ICS, variable reset to it previous value after return from another activity.

I have read that tutorial, and if not mistaken, no mention about different behavior on JB vs ICS.

And who knows how it works on another OS ver?
 
Last edited:
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
I have read that tutorial, and if not mistaken, no mention about different behavior on JB vs ICS.
And who knows how it works on another OS ver?
You are right. We were surprised too when JB arrived. But, that's the Android's life :)
Nothing is 100% sure and you'll have to accommodate every day : not on the same phone, not on the same Android's version, not the same bluetooth devices, etc.
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
Yeah, this thing gave a headache, especially for me as a noob :)
Spent few hours to find out the root of the problem.

I afraid, this is cause by B4A, it's not a bug, but an anomaly that users should be warn.
Or perhaps, this behavior just find now?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Yeah, this thing gave a headache, especially for me as a noob :)
Spent few hours to find out the root of the problem.

I afraid, this is cause by B4A, it's not a bug, but an anomaly that users should be warn.
Or perhaps, this behavior just find now?
There will be different dynamics on a different OS/device/configuration.
Just because it works okay, in one scenario does not mean you have coded it correctly.
What you are experiencing is because on your ICS devices either you have lower memory, more aggressive memory management or for what ever reason, your activity is being killed. This is default behaviour. It may not happen on your JB device as it has more memory or better memory management.
It is up to the developer to take care of Globals and State, which is why you have Activity_Pause and Resume.
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
Anyone here willing to test the last attached file, err.zip on ICS, and tells the result?

On my ICS device, non process global var didn't retain its value.
The sample is very simple, but I don't know how much memory consume by it. I have only ICS device with 512mb RAM.
 
Upvote 0
Top