-1

I developed a very simple app which just opens our companies website.

I had to program it in a way, so that it asks the user in which browser he wants to open the website, because WebView is missing some features and has some bugs.

Full code:

package XXX;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class FullscreenActivity extends AppCompatActivity
{
    public void openWebPage(String url)
    {
        Uri webpage = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        openWebPage("https://www.example.com");
        finish();   // I also tried: System.exit(0);
    }
}

The user is getting asked which browser he likes the website to be opened with. After that, the browser opens and loads the website.

Problem: The app is still open, even though I need it to get closed at this point.

enter image description here

How can I close the app after it startet the browser?

I tried finish() and even System.exit(0), both failed.

Black
  • 12,789
  • 26
  • 116
  • 196

2 Answers2

3

You can use this flag when starting your activity:

https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

This means it will be excluded from the the recents (i.e. when you call finish() it won't appear anywhere else as recently opened).

Other questions:

Remove app from recent apps programmatically

Close application and remove from recent apps/

Blundell
  • 69,653
  • 29
  • 191
  • 215
2

Your Activity is finished,it is just showing in recent apps. Doesn't mean it is open. What you need to do it to exclude your activity from recent apps. It is as simple as adding a tag in Manifest file. If you set android:excludeFromRecents="true" like shown below, it will stop the activity from showing up in recent apps list, which is what you are looking for:

<activity
    android:name=".FullscreenActivity"
    android:excludeFromRecents="true" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity> 
Harikrishnan
  • 6,759
  • 12
  • 54
  • 107