how to use startActivityForResult in Nativescript

别说谁变了你拦得住时间么 提交于 2019-12-11 07:21:56

问题


I use a extended activity in my old project, and I want to use startActivityForResult() in the public onCreate() function.

However I don't know how to solve the error

System.err: Caused by: java.lang.Exception: Failed resolving method startActivityForResult on class android.support.v4.app.FragmentActivity

the code was ok.

activity.startActivityForResult( intent, this.MY_PERMISSION_REQUEST );

the above code was in the public onCreate() function.

but when I upgraded all the npm packges, including updating android platform to v5.0.0 and android library to v28.

and update the code from

class MainActivity extends android.app.Activity {

to

class MainActivity extends android.support.v7.app.AppCompatActivity { 

the old code just not work anymore.

System.err: Caused by: java.lang.Exception: Failed resolving method startActivityForResult on class android.support.v4.app.FragmentActivity

I think maybe it's related to startActivityForResult().

I'm new to nativescript and android.

How do I use this function in my extended activity correctly?

I want to get this answer.

But if there are other workarounds will be good, too.

For example, I tried to use old code back

class MainActivity extends android.app.Activity {

the above one, but not work.

System.err: com.tns.NativeScriptException: 
System.err: Calling js method onViewAttachedToWindow failed
System.err: 
System.err: TypeError: this._context.getSupportFragmentManager is not a function
System.err: File: "file:///data/data/com.testapp/files/app/tns_modules/tns-core-modules/ui/core/view/view.js, line: 190, column: 46
System.err: 
System.err: StackTrace: 
System.err:   Frame: function:'View._getRootFragmentManager', file:'file:///data/data/com.testapp/files/app/tns_modules/tns-core-modules/ui/core/view/view.js', line: 190, column: 47
System.err:   Frame: function:'View._getFragmentManager', file:'file:///data/data/com.testapp/files/app/tns_modules/tns-core-modules/ui/core/view/view.js', line: 215, column: 32
System.err:   Frame: function:'Frame._processNextNavigationEntry', file:'file:///data/data/com.testapp/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 134, column: 28
System.err:   Frame: function:'Frame._onAttachedToWindow', file:'file:///data/data/com.testapp/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 118, column: 14
System.err:   Frame: function:'AttachListener.onViewAttachedToWindow', file:'file:///data/data/com.testapp/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 35, column: 27
System.err:

If there are any methods to make the code startActivityForResult work again will all be ok.

My AndroidManifest.xml contains

android:minSdkVersion="21"
android:targetSdkVersion="23"

Does that mean all the devices >= 21 will be accepted,

android.support.v7.app.AppCompatActivity

the doc shows added in version 25.1.0

will I need to update my AndroidManifest.xml, too?


update

the code is from https://docs.nativescript.org/core-concepts/android-runtime/advanced-topics/extend-application-activity#extending-activity

using TYPESCRIPT

@JavaProxy("org.myApp.MainActivity")
class Activity extends android.support.v7.app.AppCompatActivity {
    private _callbacks: AndroidActivityCallbacks;
    public readonly MY_PERMISSION_REQUEST = 100;

    public onCreate(savedInstanceState: android.os.Bundle): void {
        if (!this._callbacks) {
            setActivityCallbacks(this);
        }

        this._callbacks.onCreate(this, savedInstanceState, super.onCreate);

        let intent: android.content.Intent = new android.content.Intent( ( <any> android.provider.Settings ).ACTION_HOME_SETTINGS );
        intent.setData( Uri.parse( "package:" + context.getPackageName() ) );
        let activity = app.android.foregroundActivity || app.android.startActivity;
        activity.startActivityForResult( intent, this.MY_PERMISSION_REQUEST );
    }

almost all the same, but add some code for startActivityForResult.

if removing the line startActivityForResult, everything works fine again.


update ###

same error when using this.startActivityForResult()

System.err: Calling js method onCreate failed
System.err: 
System.err: Error: java.lang.Exception: Failed resolving method startActivityForResult on class android.support.v4.app.FragmentActivity
System.err:     com.tns.Runtime.resolveMethodOverload(Runtime.java:1075)
System.err:     com.tns.Runtime.callJSMethodNative(Native Method)
System.err:     com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1116)
System.err:     com.tns.Runtime.callJSMethodImpl(Runtime.java:996)
System.err:     com.tns.Runtime.callJSMethod(Runtime.java:983)
System.err:     com.tns.Runtime.callJSMethod(Runtime.java:967)
System.err:     com.tns.Runtime.callJSMethod(Runtime.java:959)
System.err:     com.testapp.MainActivity.onCreate(MainActivity.java:18)
System.err:     android.app.Activity.performCreate(Activity.java:7009)
System.err:     android.app.Activity.performCreate(Activity.java:7000)

update

my steps to creating the app

the code


回答1:


android.support.v7.app.AppCompatActivity extends from android.support.v4.app.FragmentActivity.

so if you want to use AppCompatActivity, you should include both v4 compat library and v7 compat library.




回答2:


I think due to recent changes in core modules, your activity variable will be undefined inside the create callback.

Try a timeout or use this instead, like

  this.startActivityForResult(intent, this.MY_PERMISSION_REQUEST );



回答3:


I had a similar issue to this. I posted about it here:

https://github.com/NativeScript/NativeScript/issues/7442

I managed to fix the issue moving away from using a variable, or at least, a class-scoped variable. I'm assuming in the transpilation between JavaScript and Java these are lost.

The changes, highlighted from that issue, are:

this.startActivityForResult(intent, this.REQUEST_CODE_SET_DEFAULT_DIALER);

where this.REQUEST_CODE_SET_DEFAULT_DIALER is 123

I now have this instead

this.startActivityForResult(intent, 123);

I noted you can use local variables (non-this) but using the variable on the class caused issues.



来源:https://stackoverflow.com/questions/53314046/how-to-use-startactivityforresult-in-nativescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!