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?
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.
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.
Add Intent Flag Intent. FLAG_ACTIVITY_CLEAR_TOP if you want to clear the activity stack else ignore it.
try using
android:excludeFromRecents="true"
in your Activity
in AndroidManifest.xml
.
<activity
...
android:excludeFromRecents="true">
</activity>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With