接收连续的 SEND 意图将应用程序带回前台,但不会更新 Intent
Receiving consecutive SEND intents brings app back to the front, but does not update Intent
在我正在进行的项目中,我遇到了接收外部意图(即非启动器意图)的问题。我有一个 Activity
可以使用正常的 LAUNCHER
意图启动,但它也响应来自其他应用程序的 SEND
意图,例如在 YouTube 应用中分享 link 到视频。我第一次从其他应用程序共享 link 时,我的 Activity
已创建,我可以使用 getIntent()
来获取意图的详细信息,即 EXTRA_TEXT
.如果我关闭我的 Activity
并用另一个 link 重试,那也很好。但是,如果我不关闭我的 Activity
,通过 "home" 或 "recently launched apps" 返回另一个应用程序,并分享另一个 link,我的 Activity
就会出现回到前台,但 getIntent()
导致 old 意图,而不是我认为会触发 Activity
.[=26 重启的新意图=]
我创建了一个 MCVE 来说明这个问题:
Activity
:
public class MainActivity extends ActionBarActivity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("MainActivity", "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.intent_text);
}
@Override
protected void onResume() {
Log.d("MainActivity", "onResume");
super.onResume();
Intent intent = getIntent();
String text;
if (intent.hasExtra(Intent.EXTRA_TEXT)) {
text = intent.getExtras().getString(Intent.EXTRA_TEXT);
} else {
text = "nothing";
}
tv.setText(text);
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intenttest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="IntentTest"
android:theme="@style/Theme.AppCompat" >
<activity
android:name=".MainActivity"
android:label="IntentTest" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>
</application>
</manifest>
为了完整起见,简单的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.intenttest.MainActivity" >
<TextView
android:id="@+id/intent_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
- 启动应用程序,文本视图打印 "nothing"
- 启动 YouTube 应用,分享 link,select 这个应用,应用重新启动,文本视图显示 YouTube 分享的任何文本
- 按主页
- 返回 YouTube 应用,分享不同 link,select 这个应用,应用重新启动,文本视图显示与之前相同的旧文本
- 从 "recent apps"
按返回键或滑动应用
- 返回 YouTube 应用,再分享一个 link,select 这个应用,应用重新启动(重新创建),文本视图显示 YouTube 刚刚分享的新文本
我觉得我在这里缺少一些基本的东西。如何让我的应用响应新的 Intent
?
将 android:launchMode="singleTop"
添加到 <activity>
描述中。
当 Activity 启动时(如果尚未启动 运行),Android 将调用 onCreate()
,您可以获得 Intent
通过调用 getIntent()
(就像你已经在做的那样)。
如果 Activity 在您的应用已经 运行 时启动,Android 会将其带到前台并调用 onNewIntent()
传递新的 Intent
。您应该覆盖此方法并对传递的 Intent
执行某些操作(将其保存在成员变量中或从中提取您想要的内容或其他任何内容)。之后 Android 会调用 onResume()
.
调用 getIntent()
将始终 return 最初用于启动 Activity.
的 Intent
在我正在进行的项目中,我遇到了接收外部意图(即非启动器意图)的问题。我有一个 Activity
可以使用正常的 LAUNCHER
意图启动,但它也响应来自其他应用程序的 SEND
意图,例如在 YouTube 应用中分享 link 到视频。我第一次从其他应用程序共享 link 时,我的 Activity
已创建,我可以使用 getIntent()
来获取意图的详细信息,即 EXTRA_TEXT
.如果我关闭我的 Activity
并用另一个 link 重试,那也很好。但是,如果我不关闭我的 Activity
,通过 "home" 或 "recently launched apps" 返回另一个应用程序,并分享另一个 link,我的 Activity
就会出现回到前台,但 getIntent()
导致 old 意图,而不是我认为会触发 Activity
.[=26 重启的新意图=]
我创建了一个 MCVE 来说明这个问题:
Activity
:
public class MainActivity extends ActionBarActivity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("MainActivity", "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.intent_text);
}
@Override
protected void onResume() {
Log.d("MainActivity", "onResume");
super.onResume();
Intent intent = getIntent();
String text;
if (intent.hasExtra(Intent.EXTRA_TEXT)) {
text = intent.getExtras().getString(Intent.EXTRA_TEXT);
} else {
text = "nothing";
}
tv.setText(text);
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intenttest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="IntentTest"
android:theme="@style/Theme.AppCompat" >
<activity
android:name=".MainActivity"
android:label="IntentTest" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>
</application>
</manifest>
为了完整起见,简单的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.intenttest.MainActivity" >
<TextView
android:id="@+id/intent_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
- 启动应用程序,文本视图打印 "nothing"
- 启动 YouTube 应用,分享 link,select 这个应用,应用重新启动,文本视图显示 YouTube 分享的任何文本
- 按主页
- 返回 YouTube 应用,分享不同 link,select 这个应用,应用重新启动,文本视图显示与之前相同的旧文本
- 从 "recent apps" 按返回键或滑动应用
- 返回 YouTube 应用,再分享一个 link,select 这个应用,应用重新启动(重新创建),文本视图显示 YouTube 刚刚分享的新文本
我觉得我在这里缺少一些基本的东西。如何让我的应用响应新的 Intent
?
将 android:launchMode="singleTop"
添加到 <activity>
描述中。
当 Activity 启动时(如果尚未启动 运行),Android 将调用 onCreate()
,您可以获得 Intent
通过调用 getIntent()
(就像你已经在做的那样)。
如果 Activity 在您的应用已经 运行 时启动,Android 会将其带到前台并调用 onNewIntent()
传递新的 Intent
。您应该覆盖此方法并对传递的 Intent
执行某些操作(将其保存在成员变量中或从中提取您想要的内容或其他任何内容)。之后 Android 会调用 onResume()
.
调用 getIntent()
将始终 return 最初用于启动 Activity.
Intent