Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finish() finish the activity but it is still on background

I have a question concerning the finish() method of an activity in android studio:

I have this simple code:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}

In my activity_main I simply have a button. When I click on the button the activity finishes and I go back to the main screen of the device...However, when I click on the menu button of the device (which shows all the app running in background) I still see my activity... and I do not want that. How can I fix it?

like image 756
Bazouk55555 Avatar asked Sep 16 '17 14:09

Bazouk55555


People also ask

What happens when you call finish () inside onCreate ()?

As per official documentation: You can call finish() from within this function, in which case onDestroy() will be immediately called after onCreate(Bundle) without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing.

How do you check if activity is finish or not?

Using activity. isFinishing() is the right one solution. it return true if activity is finished so before creating dialog check for the condition. if true then create and show dialog.

How do I completely finish an activity on Android?

Add Intent Flag Intent. FLAG_ACTIVITY_CLEAR_TOP if you want to clear the activity stack else ignore it.


1 Answers

try using

android:excludeFromRecents="true" in your Activity in AndroidManifest.xml.

<activity
            ...
             android:excludeFromRecents="true">
</activity>
like image 128
Mohamed Nageh Avatar answered Nov 14 '22 22:11

Mohamed Nageh