mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 10:45:23 +08:00
Fixed: Fix terminal sessions being re-added if "New Session" shortcut or termux-reload-settings was used
If TermuxActivity was recreated then the original intent was re-delivered, resulting in a new session being re-added each time. Closes #2566
This commit is contained in:
@@ -153,6 +153,13 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo
|
|||||||
*/
|
*/
|
||||||
private boolean isOnResumeAfterOnCreate = false;
|
private boolean isOnResumeAfterOnCreate = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If activity was restarted like due to call to {@link #recreate()} after receiving
|
||||||
|
* {@link TERMUX_ACTIVITY#ACTION_RELOAD_STYLE}, system dark night mode was changed or activity
|
||||||
|
* was killed by android.
|
||||||
|
*/
|
||||||
|
private boolean mIsActivityRecreated = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link TermuxActivity} is in an invalid state and must not be run.
|
* The {@link TermuxActivity} is in an invalid state and must not be run.
|
||||||
*/
|
*/
|
||||||
@@ -175,6 +182,7 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo
|
|||||||
private static final int CONTEXT_MENU_REPORT_ID = 9;
|
private static final int CONTEXT_MENU_REPORT_ID = 9;
|
||||||
|
|
||||||
private static final String ARG_TERMINAL_TOOLBAR_TEXT_INPUT = "terminal_toolbar_text_input";
|
private static final String ARG_TERMINAL_TOOLBAR_TEXT_INPUT = "terminal_toolbar_text_input";
|
||||||
|
private static final String ARG_ACTIVITY_RECREATED = "activity_recreated";
|
||||||
|
|
||||||
private static final String LOG_TAG = "TermuxActivity";
|
private static final String LOG_TAG = "TermuxActivity";
|
||||||
|
|
||||||
@@ -183,6 +191,9 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo
|
|||||||
Logger.logDebug(LOG_TAG, "onCreate");
|
Logger.logDebug(LOG_TAG, "onCreate");
|
||||||
isOnResumeAfterOnCreate = true;
|
isOnResumeAfterOnCreate = true;
|
||||||
|
|
||||||
|
if (savedInstanceState != null)
|
||||||
|
mIsActivityRecreated = savedInstanceState.getBoolean(ARG_ACTIVITY_RECREATED, false);
|
||||||
|
|
||||||
// Check if a crash happened on last run of the app and show a
|
// Check if a crash happened on last run of the app and show a
|
||||||
// notification with the crash details if it did
|
// notification with the crash details if it did
|
||||||
CrashUtils.notifyAppCrashOnLastRun(this, LOG_TAG);
|
CrashUtils.notifyAppCrashOnLastRun(this, LOG_TAG);
|
||||||
@@ -339,6 +350,7 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo
|
|||||||
|
|
||||||
super.onSaveInstanceState(savedInstanceState);
|
super.onSaveInstanceState(savedInstanceState);
|
||||||
saveTerminalToolbarTextInput(savedInstanceState);
|
saveTerminalToolbarTextInput(savedInstanceState);
|
||||||
|
savedInstanceState.putBoolean(ARG_ACTIVITY_RECREATED, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -359,15 +371,17 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo
|
|||||||
|
|
||||||
setTermuxSessionsListView();
|
setTermuxSessionsListView();
|
||||||
|
|
||||||
|
final Intent intent = getIntent();
|
||||||
|
setIntent(null);
|
||||||
|
|
||||||
if (mTermuxService.isTermuxSessionsEmpty()) {
|
if (mTermuxService.isTermuxSessionsEmpty()) {
|
||||||
if (mIsVisible) {
|
if (mIsVisible) {
|
||||||
TermuxInstaller.setupBootstrapIfNeeded(TermuxActivity.this, () -> {
|
TermuxInstaller.setupBootstrapIfNeeded(TermuxActivity.this, () -> {
|
||||||
if (mTermuxService == null) return; // Activity might have been destroyed.
|
if (mTermuxService == null) return; // Activity might have been destroyed.
|
||||||
try {
|
try {
|
||||||
Bundle bundle = getIntent().getExtras();
|
|
||||||
boolean launchFailsafe = false;
|
boolean launchFailsafe = false;
|
||||||
if (bundle != null) {
|
if (intent != null && intent.getExtras() != null) {
|
||||||
launchFailsafe = bundle.getBoolean(TERMUX_ACTIVITY.EXTRA_FAILSAFE_SESSION, false);
|
launchFailsafe = intent.getExtras().getBoolean(TERMUX_ACTIVITY.EXTRA_FAILSAFE_SESSION, false);
|
||||||
}
|
}
|
||||||
mTermuxTerminalSessionClient.addNewSession(launchFailsafe, null);
|
mTermuxTerminalSessionClient.addNewSession(launchFailsafe, null);
|
||||||
} catch (WindowManager.BadTokenException e) {
|
} catch (WindowManager.BadTokenException e) {
|
||||||
@@ -379,10 +393,12 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo
|
|||||||
finishActivityIfNotFinishing();
|
finishActivityIfNotFinishing();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Intent i = getIntent();
|
// If termux was started from launcher "New session" shortcut and activity is recreated,
|
||||||
if (i != null && Intent.ACTION_RUN.equals(i.getAction())) {
|
// then the original intent will be re-delivered, resulting in a new session being re-added
|
||||||
|
// each time.
|
||||||
|
if (!mIsActivityRecreated && intent != null && Intent.ACTION_RUN.equals(intent.getAction())) {
|
||||||
// Android 7.1 app shortcut from res/xml/shortcuts.xml.
|
// Android 7.1 app shortcut from res/xml/shortcuts.xml.
|
||||||
boolean isFailSafe = i.getBooleanExtra(TERMUX_ACTIVITY.EXTRA_FAILSAFE_SESSION, false);
|
boolean isFailSafe = intent.getBooleanExtra(TERMUX_ACTIVITY.EXTRA_FAILSAFE_SESSION, false);
|
||||||
mTermuxTerminalSessionClient.addNewSession(isFailSafe, null);
|
mTermuxTerminalSessionClient.addNewSession(isFailSafe, null);
|
||||||
} else {
|
} else {
|
||||||
mTermuxTerminalSessionClient.setCurrentSession(mTermuxTerminalSessionClient.getCurrentStoredSessionOrLast());
|
mTermuxTerminalSessionClient.setCurrentSession(mTermuxTerminalSessionClient.getCurrentStoredSessionOrLast());
|
||||||
@@ -541,7 +557,7 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo
|
|||||||
});
|
});
|
||||||
|
|
||||||
findViewById(R.id.toggle_keyboard_button).setOnLongClickListener(v -> {
|
findViewById(R.id.toggle_keyboard_button).setOnLongClickListener(v -> {
|
||||||
toggleTerminalToolbar();
|
//toggleTerminalToolbar();
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -808,6 +824,10 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo
|
|||||||
return isOnResumeAfterOnCreate;
|
return isOnResumeAfterOnCreate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isActivityRecreated() {
|
||||||
|
return mIsActivityRecreated;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public TermuxService getTermuxService() {
|
public TermuxService getTermuxService() {
|
||||||
|
@@ -105,7 +105,7 @@ public class TermuxTerminalViewClient extends TermuxTerminalViewClientBase {
|
|||||||
*/
|
*/
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
// Show the soft keyboard if required
|
// Show the soft keyboard if required
|
||||||
setSoftKeyboardState(true, false);
|
setSoftKeyboardState(true, mActivity.isActivityRecreated());
|
||||||
|
|
||||||
mTerminalCursorBlinkerStateAlreadySet = false;
|
mTerminalCursorBlinkerStateAlreadySet = false;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user