最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - Android: Restrict application to a single instance when opening files from any application - Stack Overflow

programmeradmin1浏览0评论

I have an application that has an Intent filter to open files of type PDF using it. My app lets users upload the pdf when it's opened with my app within the same logged in session. If a new instance of the app is created, the user has to go through the inconvenience of logging in again.

The issue is that despite having android:launchMode="singleTask" in the Manifest, I arrive at a different instance(Task) of my application when sharing pdf file using certain applications. Ex: If a pdf is shared from FX file explorer app, it opens the pdf in the same instance(desired), but when the same pdf is opened using Google's Files application, it opens as a new Task on top of the existing Files instance.

I have an application that has an Intent filter to open files of type PDF using it. My app lets users upload the pdf when it's opened with my app within the same logged in session. If a new instance of the app is created, the user has to go through the inconvenience of logging in again.

The issue is that despite having android:launchMode="singleTask" in the Manifest, I arrive at a different instance(Task) of my application when sharing pdf file using certain applications. Ex: If a pdf is shared from FX file explorer app, it opens the pdf in the same instance(desired), but when the same pdf is opened using Google's Files application, it opens as a new Task on top of the existing Files instance.

Share Improve this question asked Feb 6 at 6:08 HarshaHarsha 1,7462 gold badges20 silver badges23 bronze badges 3
  • When you open a PDF file via Google Files, an Intent containing FLAG_ACTIVITY_NEW_TASK is automatically sent, forcing Android to launch a new instance of your app in a separate task, even if you use singleTask in your AndroidManifest.xml. When you open from an app like FX File Explorer, the Intent may be sent without this flag, allowing the same task that is already open to be reused. – farouk osama Commented Feb 6 at 6:35
  • @faroukosama How to prevent my application from starting in a new task, but to open it in the existing window forcefully? I earlier thought singleTask parameter would suffice – Harsha Commented Feb 6 at 7:04
  • Use isTaskRoot() in onCreate() to redirect to the original task when needed, or use taskAffinity to prevent entirely new tasks from being created. – farouk osama Commented Feb 8 at 7:50
Add a comment  | 

1 Answer 1

Reset to default 0

try this:

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (!isTaskRoot()) { 
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
        finish();
        return;
    }  }

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论