简体   繁体   中英

How Can I call ShowForm from android native code?

I am using codenameone where I need to do some native code in android but that invloves changing the content view and when I try to get the application to return to the codenameone forms , using the native code I end up with white screen ,

is there anyway that I could use showForm that codenameone handles navigation through from android native code ??

After trying the bellow comments here is the log cat for the app while simulating the case :

??-?? ??:??:??.???: INFO/<unknown>(<unknown>): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.mycompany.myapp/.MyApplicationStub bnds=[540,672][786,960]} from uid 10010 on display 0 from pid 1738 ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): Start proc 31254:com.mycompany.myapp/u0a352 for activity com.mycompany.myapp/.MyApplicationStub ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): Gaining focus: Window{252c6842 u0 com.mycompany.myapp/com.mycompany.myapp.MyApplicationStub} ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): [AppLaunch] Displayed Displayed com.mycompany.myapp/.MyApplicationStub: +335ms ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): Thread[1,tid=31254,WaitingForJniOnLoad,Thread*=0x7facc9a000,peer=0x760befb0,"main"] recursive attempt to load library "/data/app/com.mycompany.myapp-1/lib/arm64/libopentok.so" ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): Thread[1,tid=31254,WaitingForJniOnLoad,Thread*=0x7facc9a000,peer=0x760befb0,"main"] recursive attempt to load library "/data/app/com.mycompany.myapp-1/lib/arm64/libopentok.so" ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): Thread[1,tid=31254,WaitingForJniOnLoad,Thread*=0x7facc9a000,peer=0x760befb0,"main"] recursive attempt to load library "/data/app/com.mycompany.myapp-1/lib/arm64/libopentok.so" ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): Thread[1,tid=31254,WaitingForJniOnLoad,Thread*=0x7facc9a000,peer=0x760befb0,"main"] recursive attempt to load library "/data/app/com.mycompany.myapp-1/lib/arm64/libopentok.so" ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): Thread[1,tid=31254,WaitingForJniOnLoad,Thread*=0x7facc9a000,peer=0x760befb0,"main"] recursive attempt to load library "/data/app/com.mycompany.myapp-1/lib/arm64/libopentok.so" ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): setMode: mode = 3; packageName = com.mycompany.myapp ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): setMode: mode = 0; packageName = com.mycompany.myapp ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): at com.mycompany.myapp.VideoChat$1.run(Unknown Source) ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): Process: com.mycompany.myapp, PID: 31254 ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): at com.mycompany.myapp.NativeVideoChatImpl$VideoChat$4.onClick(NativeVideoChatImpl.java:234) ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): Force finishing activity 1 com.mycompany.myapp/.MyApplicationStub ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): Losing focus: Window{252c6842 u0 com.mycompany.myapp/com.mycompany.myapp.MyApplicationStub} ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): Process com.mycompany.myapp (pid 31254) has died ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): channel '252c6842 com.mycompany.myapp/com.mycompany.myapp.MyApplicationStub (server)' ~ Consumer closed input channel or an error occurred. events=0x9 ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): channel '252c6842 com.mycompany.myapp/com.mycompany.myapp.MyApplicationStub (server)' ~ Channel is unrecoverably broken and will be disposed! ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): WIN DEATH: Window{252c6842 u0 com.mycompany.myapp/com.mycompany.myapp.MyApplicationStub} ??-?? ??:??:??.???: INFO/<unknown>(<unknown>): Attempted to unregister already unregistered input channel '252c6842 com.mycompany.myapp/com.mycompany.myapp.MyApplicationStub (server)'

See the advanced section in the developer guide titled "Native Code Callbacks".

Create a static method eg showForm() in the class where you create the form. In your native Android code you can call that method using the full package name of the class that implements the method eg: com.mycompany.myapp.MyFormClass.showForm() .

The class that implements the method then needs to have a reference to the form, so it could be the class that creates the form and keep a static reference to the form. To preserve memory, you may want to release the reference if the form is dismissed.

In iOS and other platforms follow the guidelines in the developer guide eg in this case for iOS showForm() would look like this (at the top of your .m file:

#include "com_mycompany_myapp_MyFormClass.h"

Then when you want to call showForm() :

com_mycompany_myapp_MyFormClass_showForm__(CN1_THREAD_STATE_PASS_SINGLE_ARG);

After trying all the senarios , I noticed that it actually get back to the form but the contents was not shown , so here is what I did since I encountered this issue in another app , the code was implemented in the native side -- tried to change the time period and when I go less the problem returns so that is good enough for me

getActivity().finish();
            intent = new Intent(getContext(), MyApplicationStub.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
            handler = new Handler();
            timer = new Timer();    
            initializeTimerTask();
            timer.schedule(timerTask, 500);

public void initializeTimerTask() {

            timerTask = new TimerTask() {
                public void run() {

                    //use a handler to run a toast that shows the current timestamp
                    handler.post(new Runnable() {
                        public void run() {
                            getContext().startActivity(intent);
                        }
                    });
                }
            };
        }

Thanks to all your Help @JamesH and @Shai

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM