3

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.
Bill
  • 65
  • 1
  • 6
  • 1
    possible duplicate of https://stackoverflow.com/a/14523375/5492047 – Reyansh Mishra Nov 10 '17 at 19:14
  • Possible duplicate of [Android - How to remove activity from recent apps?](https://stackoverflow.com/questions/14523287/android-how-to-remove-activity-from-recent-apps) – Reyansh Mishra Nov 10 '17 at 19:14
  • @ReyanshMishra ... Before jumping to conclusion, please understand the question. I already tried the solution provided in your pointers, it does not work in this case. Try it yourself, create empty activity, add a button, call finish in button click listener. Then suggest a duplicate if it works for you. – Bill Nov 10 '17 at 19:38
  • The reason I flagged it in your mentioned manifest I did not find android:excludeFromRecents="true" and it works for me I just tried it have a look https://www.youtube.com/watch?v=YoK4mZ7kFAY&feature=youtu.be. There can be one difference after finish can you call System.exit(0); and in the demo to exit the app I am clicking on the search button. – Reyansh Mishra Nov 10 '17 at 19:52

4 Answers4

5

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.

Reyansh Mishra
  • 1,879
  • 1
  • 14
  • 26
2

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

in your Activity TAG.

<activity
            ...
             android:excludeFromRecents="true">
</activity>
Rodrigo Gontijo
  • 587
  • 1
  • 9
  • 21
  • Thank you, but adding that line in androidmanifest.xml does not work. It still remains. This is all just basic code, I open Android Studio, selected empty activity, added a button and on button click listener added finish(). That' all. – Bill Nov 10 '17 at 19:34
  • If you want to close application completely you should use finishAffinity(); instead of finish() . It will clear all stack of activities previously opened by an application. And if you want to remove the process: int pid = android.os.Process.myPid(); android.os.Process.killProcess(pid); – Rodrigo Gontijo Nov 10 '17 at 21:00
  • After taking closer look at where to put this line, I found that it had to be within . I was putting it between and . After that correction, it worked as expected. – Bill Nov 17 '17 at 19:33
1

You could try this

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

That's Enam
  • 286
  • 2
  • 3
  • 16
1

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);
Sarmad
  • 31
  • 4