Android Question How to handle Activity_Keypress with animation

Chris Guanzon

Active Member
Licensed User
Longtime User
How is KeyPress related to custom animation?

You can always add whatever code you like to Activity_KeyPress in the main module.

I used this animation in my previous app, and i want to use this in b4xpages too. I am using an animation right to left if new activity is loaded and kill the previous activity and left to right when the user want to return to the previous activity using Activity_Keypress.
How can i add this animation to Activity_Keypress with b4xpage?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
 
Upvote 0

Chris Guanzon

Active Member
Licensed User
Longtime User

i already tried this, but how can i use this by pressing the back key (Activity_Keypress)?
 
Upvote 0

Chris Guanzon

Active Member
Licensed User
Longtime User
I tried using the B4XPage_CloseRequest with animation but i have an error.

This is the code in my B4XMainPage, it will animate the B4XLoginPage from right to left::


B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private LoginPage As B4XLoginPage
End Sub

Public Sub Initialize
   
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    LoginPage.Initialize
    B4XPages.AddPage("LoginPage", LoginPage)
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Sub Button1_Click
    B4XPages.ShowPageAndRemovePreviousPages("LoginPage")
    LoginPage.AnimateFromRight
End Sub

Public Sub AnimateFromLeft
    #if B4A
    Root.Left = 100%x
    Root.SetLayoutAnimated(300, 0, 0, Root.Width, Root.Height)
    #End If
End Sub

And this is the code in B4XLoginPage, it will animate the B4XMainPage from left to right

B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private ScrollView1 As B4XView
    Private Panel1 As B4XView
    Private MainPage As B4XMainPage
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'load the layout to Root
    Root.LoadLayout("LoginPage")
    MainPage.Initialize
    B4XPages.AddPage("MainPage", MainPage)
   
    ScrollView1.ScrollViewInnerPanel.LoadLayout("LoginPage_Item")
    ScrollView1.ScrollViewContentHeight = 100%y
End Sub

Sub B4XPage_CloseRequest As ResumableSub
    B4XPages.ShowPageAndRemovePreviousPages("MainPage")
    MainPage.AnimateFromLeft
    Return True
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Public Sub AnimateFromRight
    #if B4A
    Root.Left = -100%x
    Root.SetLayoutAnimated(300, 0, 0, Root.Width, Root.Height)
    #End If
End Sub

This is the error i received:

B4X:
** Activity (main) Resume **
Page with this id already exists: mainpage!
Error occurred on line: 35 (B4XMainPage)
java.lang.RuntimeException: Object should first be initialized (B4XView).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
    at anywheresoftware.b4a.objects.B4XViewWrapper.asViewWrapper(B4XViewWrapper.java:88)
    at anywheresoftware.b4a.objects.B4XViewWrapper.setLeft(B4XViewWrapper.java:123)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at b4a.example.b4xloginpage._b4xpage_closerequest(b4xloginpage.java:80)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:318)
    at anywheresoftware.b4a.debug.Debug.CallSubNew(Debug.java:282)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.keywords.Common.CallSubDebug(Common.java:1050)
    at b4a.example.b4xpagesmanager$ResumableSub_HandleCloseRequest.resume(b4xpagesmanager.java:811)
    at b4a.example.b4xpagesmanager._handlecloserequest(b4xpagesmanager.java:781)
    at b4a.example.b4xpagesmanager._activity_keypress(b4xpagesmanager.java:539)
    at b4a.example.b4xpagesdelegator._activity_keypress(b4xpagesdelegator.java:63)
    at b4a.example.main._activity_keypress(main.java:424)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at b4a.example.main$HandleKeyDelayed.runDirectly(main.java:231)
    at b4a.example.main$HandleKeyDelayed.run(main.java:228)
    at android.os.Handler.handleCallback(Handler.java:836)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:203)
    at android.app.ActivityThread.main(ActivityThread.java:6251)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
** Activity (main) Pause event (activity is not paused). **
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Chris Guanzon

Active Member
Licensed User
Longtime User
You should never create duplicate pages instances.

See the three pages example: [B4X] B4XPages - Cross platform and simple framework for managing multiple pages

Remove:
B4X:
    MainPage.Initialize
    B4XPages.AddPage("MainPage", MainPage)
Correct way to access the main page:
B4X:
B4XPages.MainPage
'or other pages:
Dim SomeOtherPage1 As SomeOtherPage = B4XPages.GetPage("some other page id")

Perfect! Thank you sir @Erel , this is what i want. I uploaded the project for someone who wants to try the same transition as i want.
 

Attachments

  • Project.zip
    10.1 KB · Views: 157
Upvote 0
Top