android - 完成当前 Activity 并开始新 Activity

标签 android

我有一个 Fragment F。从服务获得结果后,F 需要完成它所在的 Activity 并在 Web 浏览器中启动一个页面,我使用 Intent 执行此操作。

如果用户按下后退按钮,它似乎工作正常。但是,如果他从最近的应用程序启动该应用程序,则该 Activity 尚未完成。 我已经考虑过这样做的其他方式。就像完成当前 Activity 并从父 Activity 打开页面一样。但我必须对流程进行大量更改。所以那将是我最后的选择。有什么方法可以确保即使我从最近的应用程序启动 Activity 也已完成?

Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
getActivity().finish();

编辑:添加代码。

最佳答案

尝试使用此代码启动浏览器并清除应用程序的所有堆栈

Intent intent  = new Intent(); // need to set your Intent View here
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);        
getActivity().startActivity(intent);
getActivity().finish();

已更新

在 AndroidManifiest.xml 文件中的 Activity 中尝试这个属性

<activity android:name=".MainActivity"
        android:excludeFromRecents="true" ...

关于android - 完成当前 Activity 并开始新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14332441/

相关文章:

java - JNI : Calling a java method from C periodically is not working

android - 应用程序在后台被杀死时的应用程序生命周期

java - 许多十六进制字符的 NumberFormatException

java - Android - 检查单选按钮已选中

android - UML 类图 : how to model relations about calling a method or starting an activity or service

android - MPAndroidChart 线帽/结束样式

android - 有没有 "match_sibling"这样的东西? (相对于 "match parent")

java - iText/云打印不适用于 Android

java - Android:无法从静态上下文中引用非静态方法。使困惑?

android - 如何在android中为按钮添加图像?