Skip to content

Commit

Permalink
keep methods in QMUIFragmentActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
cgspine committed Apr 27, 2020
1 parent 9cb0960 commit d539dcc
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 2 deletions.
13 changes: 11 additions & 2 deletions arch/src/main/java/com/qmuiteam/qmui/arch/QMUIFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -152,7 +153,7 @@ public void run() {
private OnBackPressedCallback mOnBackPressedCallback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
QMUIFragment.this.handleOnBackPressed();
QMUIFragment.this.onBackPressed();
}
};

Expand Down Expand Up @@ -914,7 +915,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
return swipeBackLayout;
}

protected void handleOnBackPressed() {
protected void onBackPressed() {
QMUIFragmentContainerProvider provider = findFragmentContainerProvider();
if(!(provider instanceof FragmentActivity) || provider.getContainerFragmentManager() == null ||
provider.getContainerFragmentManager().getBackStackEntryCount() > 1){
Expand Down Expand Up @@ -1328,6 +1329,14 @@ public boolean isVisibleToUser() {
return getUserVisibleHint() && isParentVisibleToUser();
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
return false;
}

public boolean onKeyUp(int keyCode, KeyEvent event) {
return false;
}

/**
* @return true if parentFragments is visible to user
*/
Expand Down
122 changes: 122 additions & 0 deletions arch/src/main/java/com/qmuiteam/qmui/arch/QMUIFragmentActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
Expand All @@ -31,6 +32,7 @@
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentContainerView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelStoreOwner;

import com.qmuiteam.qmui.QMUILog;
Expand All @@ -42,6 +44,8 @@
import com.qmuiteam.qmui.util.QMUIWindowInsetHelper;
import com.qmuiteam.qmui.widget.QMUIWindowInsetLayout;

import java.lang.reflect.Field;

/**
* the container activity for {@link QMUIFragment}.
* Created by cgspine on 15/9/14.
Expand Down Expand Up @@ -202,6 +206,124 @@ private QMUIFragment getCurrentQMUIFragment() {
}


/**
* start a new fragment and then destroy current fragment.
* assume there is a fragment stack(A->B->C), and you use this method to start a new
* fragment D and destroy fragment C. Now you are in fragment D, if you want call
* {@link #popBackStack()} to back to B, what the animation should be? Sometimes we hope run
* animation generated by transition B->C, but sometimes we hope run animation generated by
* transition C->D. this why second parameter exists.
*
* @param fragment new fragment to start
* @param useNewTransitionConfigWhenPop if true, use animation generated by transition C->D,
* else, use animation generated by transition B->C
*
* @deprecated use {@link QMUIFragment#startFragmentAndDestroyCurrent(QMUIFragment, boolean)}
*/
@Deprecated
public int startFragmentAndDestroyCurrent(final QMUIFragment fragment, final boolean useNewTransitionConfigWhenPop) {
final QMUIFragment.TransitionConfig transitionConfig = fragment.onFetchTransitionConfig();
String tagName = fragment.getClass().getSimpleName();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction()
.setCustomAnimations(transitionConfig.enter, transitionConfig.exit,
transitionConfig.popenter, transitionConfig.popout)
.replace(getContextViewId(), fragment, tagName);
int index = transaction.commit();
Utils.findAndModifyOpInBackStackRecord(fragmentManager, -1, new Utils.OpHandler() {
@Override
public boolean handle(Object op) {
Field cmdField = null;
try {
cmdField = Utils.getOpCmdField(op);
cmdField.setAccessible(true);
int cmd = (int) cmdField.get(op);
if (cmd == 1) {
if (useNewTransitionConfigWhenPop) {
Field popEnterAnimField = Utils.getOpPopEnterAnimField(op);
popEnterAnimField.setAccessible(true);
popEnterAnimField.set(op, transitionConfig.popenter);

Field popExitAnimField = Utils.getOpPopExitAnimField(op);
popExitAnimField.setAccessible(true);
popExitAnimField.set(op, transitionConfig.popout);
}

Field oldFragmentField = Utils.getOpFragmentField(op);
oldFragmentField.setAccessible(true);
Object fragmentObj = oldFragmentField.get(op);
oldFragmentField.set(op, fragment);
Field backStackNestField = Fragment.class.getDeclaredField("mBackStackNesting");
backStackNestField.setAccessible(true);
int oldFragmentBackStackNest = (int) backStackNestField.get(fragmentObj);
backStackNestField.set(fragment, oldFragmentBackStackNest);
backStackNestField.set(fragmentObj, --oldFragmentBackStackNest);
return true;
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
}

@Override
public boolean needReNameTag() {
return true;
}

@Override
public String newTagName() {
return fragment.getClass().getSimpleName();
}
});
return index;
}

/**
*
* @param fragment target fragment to start
* @return commit id
*
* @deprecated use {@link QMUIFragment#startFragment(QMUIFragment)}
*/
@Deprecated
public int startFragment(QMUIFragment fragment) {
Log.i(TAG, "startFragment");
QMUIFragment.TransitionConfig transitionConfig = fragment.onFetchTransitionConfig();
String tagName = fragment.getClass().getSimpleName();
return getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(transitionConfig.enter, transitionConfig.exit, transitionConfig.popenter, transitionConfig.popout)
.replace(getContextViewId(), fragment, tagName)
.addToBackStack(tagName)
.commit();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
QMUIFragment fragment = getCurrentQMUIFragment();
if (fragment != null && !fragment.isInSwipeBack() && fragment.onKeyDown(keyCode, event)) {
return true;
}
return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
QMUIFragment fragment = getCurrentQMUIFragment();
if (fragment != null && !fragment.isInSwipeBack() && fragment.onKeyUp(keyCode, event)) {
return true;
}
return super.onKeyUp(keyCode, event);
}

public void popBackStack() {
getOnBackPressedDispatcher().onBackPressed();
}


public static Intent intentOf(@NonNull Context context,
@NonNull Class<? extends QMUIFragmentActivity> targetActivity,
@NonNull Class<? extends QMUIFragment> firstFragment) {
Expand Down

0 comments on commit d539dcc

Please sign in to comment.