Android app opening browser in the same tab?
Asked Answered
S

3

2

I'm finding this hard to explain, but i'll do my best. I have an app that opens the browser (firefox) and send information from that app to the webpage as a php variable, the problem i have is that i am doing this quite a lot, and everytime it opens the browser its opening a new tab.

I know there is no way to close a tab using javascript etc, so is there a way to ensure it always opens into the same current tab so i dont end up with several open at once.

I dont want to keep having to close firefox tabs whenever the app fires up the browser.

Sorry if its hard to make sense of. Thanks.

Saleable answered 24/4, 2016 at 13:31 Comment(0)
P
3

Probably this is a bit late, but I have managed to accomplish what you want only using Chrome app.

String url = "http://www.mypage.com";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.putExtra(Browser.EXTRA_APPLICATION_ID, "com.android.chrome");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

This allows you to re-use the current opened tab on Chrome and change the current URL in that tab to the one you are providing in the intent.

I also have tried with no success on Firefox.

I hope this helps.

Pyroxene answered 8/9, 2016 at 16:19 Comment(0)
N
1

For Firefox android app to replace the existing url of current tab.

String package = "org.mozilla.firefox";
String url = "http://www.test.com";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setPackage(package);
intent.putExtra(Browser.EXTRA_APPLICATION_ID, package);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

For chrome or other browsers, just change the package name. Hopefully it will solve the problem.

Nib answered 24/7, 2019 at 12:15 Comment(0)
S
0

Answer used to open a "sample.html" file in same tab instead of creating a new tab of Google Chrome browser.

       btnClickMe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            File file = new File(Environment.getExternalStorageDirectory(), "sample.html");
            Uri uri2 = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri2);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setClassName("com.android.chrome", "com.google.android.apps.chrome.Main");
            intent.putExtra(Browser.EXTRA_APPLICATION_ID, "com.android.chrome");
            try {
                startActivity(intent);
            } catch (Exception e) {
                Log.e("Exception ", e.getLocalizedMessage());
            }
        }
    });

Note : Kindly check your Storage permission before proceeding.

Schug answered 14/10, 2019 at 7:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.