我的处境很奇怪。
有了一个应用程序,我决定从第一个应用的代码中创建另一个应用程序。
我复制了.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);
}
}不幸的是,我没有在日志中看到“我工作过”。
我在网上和网上都读过很多类似的问题,我试着设置意图标志SINGLE_TOP、SINGLE_TASK等等。
这是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());终于成功了!
我不知道,这是一个肮脏的解决方法,还是一个错误,如果有人能解释,请评论。
发布于 2016-04-29 15:25:46
序言:
好吧,我对这个问题有点晚了,但是当我在同一个问题上绊倒了,这里没有答案,或者其他四个堆叠溢出问题中的任何一个,我找到了这个问题,为我解决了这个问题,以下是我想出的办法。
答案:
有几个可能的原因,为什么onNewIntent没有被调用,我将列出它们--好吧,我知道所有这些。
android:launchMode="singleTop"清单条目,您希望在其中调用onNewIntent,或者用于启动活动的意图必须有标志FLAG_ACTIVITY_SINGLE_TOP。你不需要两者都需要(这是一个| aka。逻辑or,而不是&。逻辑and )!也应该为onNewIntent调用android:launchMode="singleTask",但是在使用它之前,最好先查看android文档,因为它有更多的后果,而不仅仅是一个函数调用。android:launchMode="singleTop"按指定的方式工作,从而阻止了对onNewIntent的调用。这个bug从未被正式解决,但我无法在4.4.2版(三星S4 Mini)中复制它。因此,它似乎已经固定在4.0.x和4.4.2之间的某个点。,这意味着,如果活动是新创建的,那么无论您设置了什么launchMode或意图标志,都不会调用onNewIntent!
- To my understanding, either onCreate or onNewIntent is called, but never both.
- So, if you wanna pass data to the activity through the Intent and it should always work (as in my case), no matter if the activity is relaunched or the activity is freshly created, you can do as described in [this very useful blog post](http://www.helloandroid.com/tutorials/communicating-between-running-activities).
- As a variation of the solution described in the above blog post, you could also take advantage of the fact, that no matter if onNewIntent or onCreate was called, onResume will always be called afterwards, and do something like this:@覆盖受保护的无效onNewIntent(意图意图){super.onNewIntent(意图);setIntent(意图);}@重写受保护的空洞onResume() { super.onResume();意图= getIntent();// .用意图做你想做的事}
对于本例,getIntent将始终为您获得用于startActivity调用或通知的意图,因为如果活动是新创建的(因此调用了onCreate ),则将为该活动设置新的意图。
POSTAMBLE:
很抱歉寄了这么长的邮筒。我希望你在里面找到了有用的东西。
发布于 2014-05-03 15:14:01
您希望在其中接收onNewIntent()的活动应该有
android:launchMode="singleTop"或添加标志tn意图。
browserInt.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_SINGLE_TOP);如onNewIntent(意图)中所记载的
发布于 2016-09-13 16:51:24
这里有一种情况可能会咬你一口,还有一些更多的信息:onNewIntent是按预期调用的,如下代码所示:
Intent intent = new Intent...
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
finish();
startActivity(intent);但不是用这个代码
Intent intent = new Intent...
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
finish();原因是,在第二个代码块中,在调用当前活动的finish之前将新活动添加到顶部,因此,要使第二个代码块正常工作,必须按照其他一些答案所建议的那样添加FLAG_ACTIVITY_CLEAR_TOP标志,如下所示:
Intent intent = new Intent...
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);请注意,这里根本没有调用finish,因为它是为您调用的。来自顶旗
如果设置了,并且正在启动的活动已经在当前任务中运行,那么不再启动该活动的新实例,它之上的所有其他活动都将被关闭,并且该意图将作为新的意图传递给(现在位于顶部)旧活动。
https://stackoverflow.com/questions/23446120
复制相似问题