Bill
Bill

Reputation: 65

Close android activity (completely, not even in background) using finish on a button click

I need to close an activity when a button is clicked. Unfortunately, when button is clicked, the activity does disappear but is still in the background. User can still select it and it comes back to front. What I need is the activity completely gone/destroyed.

public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Button button = (Button)findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

I searched on SO on related questions, however, none of them help with closing the activity completely. I already tried adding return, adding another broadcast listener and passing command to call finish outside onCreate. So at this point, the question is - is this really possible or is this how Android works, you can call finish() but it is still in the background and user can re-launch it.

Here is xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app1.test.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

EDIT: Adding android:excludeFromRecents="true" does not solve the issue. Here are steps to recreate this, if anyone thinks it is a duplicate or already solved, please try and post comment/answer, I will accept it.

  1. Open Android Studio
  2. Create empty activity project.
  3. Add a button.
  4. Add code in MainActivity's onCreate for button click listener.
  5. Inside click listener, call finish.
  6. Run the app and click button and see if the activity is still in background or not.

Upvotes: 3

Views: 9641

Answers (4)

Sarmad
Sarmad

Reputation: 31

plz try this to go back

public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Button button = (Button)findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }
}

and this code to kill app process

moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);

Upvotes: 1

That&#39;s Enam
That&#39;s Enam

Reputation: 286

You could try this

android:excludeFromRecents="true", Use it in your manifest.

Upvotes: 1

Reyansh Mishra
Reyansh Mishra

Reputation: 1915

Just give it a try. In you manifest.

    <activity
        android:excludeFromRecents="true"
        android:name=".Activities.SplashActivity"
        android:theme="@style/AppThemeSubActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Now in your java code.

     @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.item_search:
    //                Intent intent = new Intent(MainActivity.this, SearchActivity.class);
    //                startActivity(intent);
                    finish();
                    System.exit(0);}
}

put the extra line System.exit(0); after calling finish it works for me.

Upvotes: 5

Rodrigo Gontijo
Rodrigo Gontijo

Reputation: 587

You need to put this in your XML manifest:android:excludeFromRecents="true"

in your Activity TAG.

<activity
            ...
             android:excludeFromRecents="true">
</activity>

Upvotes: 2

Related Questions