调用onCreate()而不是onActivityResult()

问题描述 投票:0回答:1

所以我使用浏览器中的OAuth通过GitHub帐户验证用户身份。但是在我从Intent发射LoginActivity后,onResume()在用户打开浏览器之前执行,因此返回uri的值等于null

这是LoginActivity

public class LoginActivity extends AppCompatActivity {

    Button loginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        loginButton = findViewById(R.id.button);
        final String url = "oauth url...";
        Log.d("called","oncreate");
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("called","onclick");
                attemptLogin(url);
            }
        });
    }

    private void attemptLogin(String url){
        Log.d("called","onattempt");
        Intent intent = new Intent(this, BrowserActivity.class);
        intent.putExtra("URL", url);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Uri uri = data.getData();
        String code = uri.getQueryParameter("code");
        Log.d("called",code);
    }
}

BrowserActivity

public class BrowserActivity extends AppCompatActivity {

    public static String CALLBACK_URL = "callback url..";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browser);
        Intent intent = getIntent();
        if(intent != null){
            String url = intent.getStringExtra("URL");
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(i);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Uri uri = getIntent().getData();
        if(uri != null && uri.toString().startsWith(CALLBACK_URL)) {
            String code = uri.getQueryParameter("code");
            Intent intent = new Intent();
            intent.putExtra("code",code);
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}

LoginActivityAndroidManifest.xml

<activity
    android:name=".ui.activities.LoginActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".ui.activities.BrowserActivity"
          android:label="@string/app_name"
          android:theme="@style/AppTheme.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data
            android:host="callback"
            android:scheme="app"
            />
    </intent-filter>
</activity>
android github github-api
1个回答
2
投票

首先,您启动浏览器

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivityForResult(intent, 1);

此意图不会返回任何结果。它只请求浏览器显示网址。因此浏览器不会返回任何结果。但是,在验证之后,浏览器会触发一个intent并请求一个新的LoginActivity实例。当然会调用onCreate()

它就像LoginActivity(#1) - > Browser - > LoginActivity(#2)

你可以在onCreate()里面调用这部分代码,它会起作用:

Uri uri = getIntent().getData();
String code = uri.getQueryParameter("code");
Log.d("called",code);

但是两个不同的意图(启动器和action_view)可以为两个不同的目的达到相同的活动。你最好创建两个不同的活动。首先是你常用的LoginActivity和第二个(继承自第一个)BrowserActivity,它重定义onCreate。到目前为止,你的Manifest看起来像

<activity
android:name=".ui.activities.LoginActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ui.activities.BrowserActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">

<intent-filter>
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <data
        android:host="callback"
        android:scheme="app"
        />
</intent-filter>
</activity>

LoginActivity代码应如下所示:

public class LoginActivity extends AppCompatActivity {

Button loginButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    loginButton = findViewById(R.id.button);
    final String url = "oauth url...";
    Log.d("called","oncreate");
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("called","onclick");
            attemptLogin(url);
        }
    });
}

private void attemptLogin(String url){
    Log.d("called","onattempt");

        // open the browser with url

        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(i);
}

// no onResult 

}

这是BrowserActivityCode:

public class BrowserActivity extends LoginActivity {

// this will be triggered after authentification
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    processIntent();
}


protected void processIntent() {
    Intent intentFromBrowser = getIntent();
    if (intentFromBrowser != null) {
        Uri uri = intentFromBrowser.getData();
    if(uri != null && uri.toString().startsWith(CALLBACK_URL)) {
        String code = uri.getQueryParameter("code");
        Log.d("code value", code);
    }
    }
}
}
© www.soinside.com 2019 - 2024. All rights reserved.