how to use startActivityForResult in Nativescript

2019-08-30 01:36发布

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

3条回答
Luminary・发光体
2楼-- · 2019-08-30 02:16

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.

查看更多
3楼-- · 2019-08-30 02:26

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.

查看更多
聊天终结者
4楼-- · 2019-08-30 02:29

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 );
查看更多
登录 后发表回答