[JAVA] [Android] Exit the activity of the transition source at the time of screen transition

When creating an app on Android, you may want to close the app with the back button.

Ex) Exit the app by pressing the back button after the transition from the login screen. (Do not return to the login screen)

In such a case, we have summarized how to terminate the Activity.

At the time of transition from Activity to Activity

MainActivity.java


Intent intent = new Intent(getApplication(), SubActivity.class);
startActivity(intent);
finish();

When the screen A → B → C transitions, the activities of A and B are terminated (when you want to terminate multiple activities)

Ex) When you exit the app with the back button after signing in with A (initial screen) → B (sign-in screen) → C (main screen).

BActivity.java


  Intent intent = new Intent(getApplication(), CActivity.class);
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(intent);

With finish (), if you press the back button on the main screen, A (initial screen) will be displayed. This is because Activity is closed only on the sign-in screen.

FLAG_ACTIVITY_NEW_TASK:Create a new task and add the activity to launch to the task's stack
FLAG_ACTIVITY_CLEAR_TASK:Destroy an existing task before launching the Activity. This flag is FLAG_ACTIVITY_NEW_Can only be used in combination with TASK.

Exit Activity on transition from Fragment

currentFragment.java


Intent intent = new Intent(getActivity(), SecondActivity.class);
                                startActivity(intent);
                                MainActivity mainActivity = (MainActivity) getActivity();
                                mainActivity.finish();

Since finish () is not available in Fragment, you need to instantiate and use Activity.

Summary

Basically, you can finish the activity with finish ().

For example, if you log out and press the back button after transitioning to the login screen, you do not want to be returned to the logout screen, so I think you can use this implementation!

reference

I want to prevent returning to the login screen after logging in to Android

Recommended Posts

[Android] Exit the activity of the transition source at the time of screen transition
Android application: Let's explain the mechanism of screen transition with simple code
[Java] How to get the URL of the transition source
Speed comparison at the time of generation at the time of date conversion
Android development ~ Screen transition (intent) ~
Let's take a look at the screen of Quant Analyzer!
Animation settings at screen transition
About the behavior at the time of error of Files.copy (pathA, pathB)
[Rails] How to get the URL of the transition source and redirect
[Rails] About Uglifier :: Error: Unexpected token: at the time of deployment
About the basics of Android development
I read the source of ArrayList I read
I read the source of Integer
I read the source of Long
Pre-written source code for the activity
I read the source of Short
I read the source of Byte
I read the source of String
What you are doing in the confirmation at the time of gem update
[Android] Add an arbitrary character string at the beginning of multiple lines
The story of releasing the Android app to the Play Store for the first time.
Email sending function with Action Mailer at the time of new registration
How to implement the email authentication function at the time of user registration
Understand the basics of Android Audio Record
Fix the view screen of the post page
Screen transition by [Android Studio] [Java] button
[Rails 6] Change redirect destination at the time of new registration / login by devise
[Heroku] At the time of deployment We're sorry, but something went wrong. Solution