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

test: Android framents reordering #13400

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package org.appcelerator.titanium.view;

import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiConvert;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiConvert;

import java.util.ArrayList;

Expand All @@ -23,6 +24,7 @@ public abstract class TiUIFragment extends TiUIView implements Handler.Callback
private boolean fragmentCommitted = false;
protected boolean fragmentOnly = false;
private ArrayList<TiUIView> childrenToRealize = new ArrayList<>();
FragmentManager manager = null;

public TiUIFragment(TiViewProxy proxy, Activity activity)
{
Expand All @@ -45,15 +47,30 @@ public boolean dispatchTouchEvent(MotionEvent ev)
return interceptTouchEvent(ev) || super.dispatchTouchEvent(ev);
}

@Override
protected void onDetachedFromWindow()
{
if (manager != null && !manager.isDestroyed()) {
removeMyFragments();
manager.executePendingTransactions();
}

super.onDetachedFromWindow();
transactionCommitted = false;

int size = getChildCount();
for (int i = size - 1; i >= 0; i--) {
removeViewAt(i);
}
}

@Override
protected void onAttachedToWindow()
{
super.onAttachedToWindow();
if (!transactionCommitted) {
transactionCommitted = true;
FragmentManager manager = ((FragmentActivity) getContext()).getSupportFragmentManager();
if (manager != null) {
FragmentTransaction transaction = manager.beginTransaction();
transaction.runOnCommit(onCommitRunnable);
transaction.setReorderingAllowed(true);
fragment = createFragment();
transaction.add(getId(), fragment);
transaction.commitAllowingStateLoss();
Expand All @@ -62,6 +79,24 @@ protected void onAttachedToWindow()
};
container.setId(View.generateViewId());
setNativeView(container);

if (manager == null) {
manager = ((FragmentActivity) activity).getSupportFragmentManager();
}
}
}

private void removeMyFragments()
{
FragmentTransaction transaction = manager.beginTransaction();
boolean hasFragments = false;

for (Fragment fragment : manager.getFragments()) {
transaction.remove(fragment);
hasFragments = true;
}
if (hasFragments) {
transaction.commitNowAllowingStateLoss();
}
}

Expand Down Expand Up @@ -143,20 +178,17 @@ protected boolean interceptTouchEvent(MotionEvent ev)
@Override
public void release()
{
if (fragment != null) {
FragmentManager fragmentManager = fragment.getFragmentManager();
if (fragmentManager != null) {
FragmentTransaction transaction = null;
Fragment tabFragment = fragmentManager.findFragmentById(android.R.id.tabcontent);
if (tabFragment != null) {
FragmentManager childManager = fragment.getActivity().getSupportFragmentManager();
transaction = childManager.beginTransaction();
} else {
transaction = fragmentManager.beginTransaction();
}
transaction.remove(fragment);
transaction.commit();
if (fragment != null && manager != null) {
FragmentTransaction transaction = null;
Fragment tabFragment = manager.findFragmentById(android.R.id.tabcontent);
if (tabFragment != null) {
FragmentManager childManager = fragment.getActivity().getSupportFragmentManager();
transaction = childManager.beginTransaction();
} else {
transaction = manager.beginTransaction();
}
transaction.remove(fragment);
transaction.commitNowAllowingStateLoss();
}
super.release();
}
Expand Down