Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-25886] Refactor Ti.App._restart() #9946

Merged
merged 4 commits into from
May 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import org.appcelerator.kroll.KrollRuntime;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.util.KrollAssetHelper;
import org.appcelerator.titanium.ITiAppInfo;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiBaseActivity;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiPlatformHelper;
Expand Down Expand Up @@ -50,6 +52,8 @@ public class AppModule extends KrollModule implements SensorEventListener
private boolean proximityState;
private int proximityEventListenerCount = 0;

private static final String APP_PATH = "Resources/app.js";

public AppModule()
{
super("App");
Expand Down Expand Up @@ -213,14 +217,23 @@ public boolean getAccessibilityEnabled()
@Kroll.method(name = "_restart")
public void restart()
{
Application app = (Application) KrollRuntime.getInstance().getKrollApplication();
Intent i = app.getPackageManager().getLaunchIntentForPackage(app.getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setAction(Intent.ACTION_MAIN);
KrollRuntime runtime = KrollRuntime.getInstance();

// prevent termination of root activity via TiBaseActivity.shouldFinishRootActivity()
TiBaseActivity.canFinishRoot = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we set canFinishRoot to false, then backing out of the first Ti.UI.Window in a restarted window will now show the launcher activity, right? We may need to reset canFinishRoot back to true after the restart.

Perhaps this shouldn't be an exposed public global and handle it via a method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

canFinishRoot returns to true on Line 229


// terminate all activities excluding root
TiApplication.terminateActivityStack();
app.startActivity(i);

// allow termination again
TiBaseActivity.canFinishRoot = true;

// restart kroll runtime
runtime.doDispose();
runtime.initRuntime();

// manually re-launch app
runtime.doRunModule(KrollAssetHelper.readAsset(APP_PATH), APP_PATH, getActivityProxy());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our TiLaunchActivity.loadActivityScript() and the resolveUrl() method it calls does some extra logic when loading a JavaScript on startup. Particularly with Alloy. We might need to do a code shift.
https://github.com/appcelerator/titanium_mobile/blob/master/android/titanium/src/java/org/appcelerator/titanium/TiLaunchActivity.java#L120

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, I'll test these changes with an Alloy app

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update, works fine with Alloy apps

}

@Kroll.method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public static void terminateActivityStack()
WeakReference<Activity> activityRef;
Activity currentActivity;

for (int i = activityStack.size() - 1; i >= 0; i--) {
for (int i = activityStack.size() - 1; i > 0; i--) {
// We need to check the stack size here again. Since we call finish(), that could potentially
// change the activity stack while we are looping through them. TIMOB-12487
if (i < activityStack.size()) {
Expand All @@ -201,11 +201,11 @@ public static void terminateActivityStack()
currentActivity = activityRef.get();
if (currentActivity != null && !currentActivity.isFinishing()) {
currentActivity.finish();
activityStack.remove(activityRef);
}
}
}
}
activityStack.clear();
}

public boolean activityStackHasLaunchActivity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ public KrollObject getContext()
public TiWindowProxy lwWindow;
public boolean isResumed = false;

public static boolean canFinishRoot = true;

private boolean overridenLayout;

public class DialogWrapper
Expand Down Expand Up @@ -1634,7 +1636,7 @@ protected void fireOnDestroy()

protected boolean shouldFinishRootActivity()
{
return getIntentBoolean(TiC.INTENT_PROPERTY_FINISH_ROOT, false);
return canFinishRoot && getIntentBoolean(TiC.INTENT_PROPERTY_FINISH_ROOT, false);
}

@Override
Expand Down