【发布时间】:2014-05-03 15:07:01
【问题描述】:
我的情况很奇怪。
有了一个应用程序,我决定从第一个应用程序的代码中创建另一个应用程序。
我复制了 .xml 文件,复制了 .java 文件,这样一切正常。
但是有一个巨大的问题:我的onNewIntent(Intent intent) 方法在第一个项目中被调用,但在第二个项目中没有被调用(代码相同!)
方法,然后可以触发,但不能立即触发
public void onClick(View arg0) {
Intent browserInt = new Intent (Intent.ACTION_VIEW,
Uri.parse("https://oauth.yandex.ru/authorize?response_type=token&client_id=zzzzz"));
browserInt.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(browserInt);
}
这里是 onNewIntent() 方法:
@Override
protected void onNewIntent(Intent intent){
System.out.println(" I WORKED!");
Uri uri = intent.getData();
if (uri!=null) {
String m = uri.toString().split("#")[1];
String[] args = m.split("&");
String arg = args[0];
String token = arg.split("=")[1];
System.out.println(token);
}
}
很遗憾,我的日志中没有看到“I WORKED”。
我在 SO 和 Internet 上阅读了很多类似的问题,尝试设置 Intent 标志 SINGLE_TOP、SINGLE_TASK 等。
这是 WORKING 项目的 Android 清单:
<application
android:name="yyy"
android:icon="@drawable/yaru_icon"
android:allowBackup="false"
android:label="xxx"
android:theme="@style/LightTheme">
<activity
android:name=".Main"
android:label="xxx"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
我很绝望,为什么类似的代码不再工作了?
编辑:我已经尝试了所有方法:SINGLE_TOP、SINGLE_INSTANCE、SINGLE_TASK..
但后来我偶尔会在另一个活动中这样做:
Main m = new Main();
m.onNewIntent(this.getIntent());
它终于奏效了!
我不知道这是一个肮脏的解决方法还是一个错误,如果有人可以解释它,请评论。
【问题讨论】:
-
当然它直接“工作”,但是从构造函数创建一个 Activity 将永远不会返回指向将要显示的 Activity 的指针。