展示多个fragment的Activiy,支持fragment之间的返回

传入需要展示的Fragment的class对象,通过反射方式创建Fragment对象展示,再次调用start(Context, Class)方法启动,可以在原来的fragment上层再显示一个,返回键可以返回上个fragment。且有切换动画

package com.android.jsbcmasterapp.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.util.Log;

import com.android.jsbcmasterapp.R;
import com.android.jsbcmasterapp.Res;

/**
 * 
 * Description: 展示activity用的宿主;
 * 调用{@link #start(Context, Class)}方法,传入需要展示的Fragment的class,
 * singleInstance启动模式,支持启动多次展示多个fragment支持返回
 *
 * Author: xucunyang
 * Time: 2021/3/4 15:45
 * 
*/ public class FragmentDisplayActivity extends BaseCompatActivity { private static final String TAG = "FragmentDisplayActivity"; private static final String KEY_FRAGMENT_NAME = "fragment_name"; private FragmentManager fragmentManager; private String mFragmentPath; private FragmentTransaction mFragmentTransaction; @Override protected void onCreate(Bundle savedInstanceState) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mFragmentPath = getIntent().getStringExtra(KEY_FRAGMENT_NAME); fromNewIntent = false; super.onCreate(savedInstanceState); } Log.i(TAG, "onCreate: "); } @Override protected int getContentViews() { return R.layout.activity_display_fragment; } @Override protected void initViews() {} @Override protected void initListener() {} @Override protected void initData() { Log.i(TAG, "initData: "); if (TextUtils.isEmpty(mFragmentPath)) { Log.w(TAG, "initData: fragmentPath is empty"); finish(); return; } Log.i(TAG, "initData: show fragment:" + mFragmentPath); try { Class clazz = Class.forName(mFragmentPath); fragment = (BaseFragment) clazz.newInstance(); fragmentManager = getSupportFragmentManager(); mFragmentTransaction = fragmentManager.beginTransaction(); if (fromNewIntent) { mFragmentTransaction.setCustomAnimations( R.anim.slide_enter, R.anim.slide_exit, R.anim.pop_left_enter, R.anim.pop_left_exit ); } mFragmentTransaction.replace(Res.getWidgetID("frame_layout"), fragment); mFragmentTransaction.addToBackStack(mFragmentPath); mFragmentTransaction.commit(); } catch (ClassNotFoundException e) { Log.w(TAG, "initData: ClassNotFoundException:" + e.getMessage()); e.printStackTrace(); } catch (IllegalAccessException e) { Log.w(TAG, "initData: IllegalAccessException:" + e.getMessage()); e.printStackTrace(); } catch (InstantiationException e) { Log.w(TAG, "initData: InstantiationException:" + e.getMessage()); e.printStackTrace(); } } private boolean fromNewIntent = false; @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); Log.i(TAG, "onNewIntent: " + intent); fromNewIntent = true; if (intent != null) { mFragmentPath = intent.getStringExtra(KEY_FRAGMENT_NAME); initData(); } else { Log.i(TAG, "onNewIntent: intent is null"); } } @Override public void onBackPressed() { Log.w(TAG, "onBackPressed: " + fragmentManager.getBackStackEntryCount()); if (fragmentManager != null && fragmentManager.getBackStackEntryCount() == 1) { finish(); } else { super.onBackPressed(); } } @Override public void finish() { FragmentManager fragmentManager = getSupportFragmentManager(); mFragmentTransaction = fragmentManager.beginTransaction(); mFragmentTransaction.setCustomAnimations(0, 0); mFragmentTransaction.commit(); super.finish(); overridePendingTransition(R.anim.pop_left_enter, R.anim.pop_left_exit); } @Override public void startActivity(Intent intent) { super.startActivity(intent); overridePendingTransition(R.anim.slide_enter, R.anim.activity_start_outgoing); } /** * 展示fragment * * @param context 上下文 * @param fragmentCls fragment */ public static void start(Context context, Class fragmentCls) { if (fragmentCls != null) { Log.i(TAG, "start: " + fragmentCls.getName()); Intent intent = new Intent(context, FragmentDisplayActivity.class); intent.putExtra(KEY_FRAGMENT_NAME, fragmentCls.getName()); context.startActivity(intent); if (context instanceof Activity) { ((Activity) context).overridePendingTransition(R.anim.slide_enter, R.anim.activity_start_outgoing); } else { Log.w(TAG, "start: context is not instance of Activity, " + context); } } else { Log.w(TAG, "start: fragment is null"); } } }

使用的切换动画文件
activity_start_outgoing.xml




    

    


pop_left_enter.xml




    

    


pop_left_exit.xml




    

    


slide_enter.xml




    

    


slide_exit.xml




    

    


你可能感兴趣的:(展示多个fragment的Activiy,支持fragment之间的返回)