public class BaseBackFlowActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        BackFlow.Logger.log(TAG, "taskId:" + getTaskId()
                + ", obj:" + Integer.toHexString(hashCode())
                + ", myPid:" + android.os.Process.myPid()  //获取当前进程的id
                + ", process:" + BackFlow.getCurProcessName(this));
    }

    public <T extends View> T $(@IdRes int id) {
        return (T) findViewById(id);
    }


    //******************** start activity replace method ********************
    public void startActivity4NonBackFlow(Intent intent) {
        startActivityForResult(intent, BackFlow.REQUEST_CODE_4_NON_BACK_FLOW);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void startActivity4NonBackFlow(Intent intent, @Nullable Bundle options) {
        startActivityForResult(intent, BackFlow.REQUEST_CODE_4_NON_BACK_FLOW, options);
    }


    //******************** back flow ********************
    @Override
    public void startActivity(Intent intent) {
        startActivityForResult(intent, BackFlow.REQUEST_CODE);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void startActivity(Intent intent, @Nullable Bundle options) {
        startActivityForResult(intent, BackFlow.REQUEST_CODE, options);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (BackFlow.handle(this, getSupportFragmentManager().getFragments(), requestCode, resultCode, data)) {
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        BackFlow.Logger.log("onDestroy", this.getClass().getSimpleName());
        super.onDestroy();
    }
} public class FcsfAFragment extends ContainsSFFragment {

    @Override
    protected void initView() {
        super.initView();
        jump2NextView(FcsfBFragment.class);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
} public class MultiFragmentActivity extends BaseBackFlowActivity {
    private String[] mTitles = new String[]{"唐僧", "大师兄", "二师兄", "沙师弟"};

    private ViewPager viewPager;
    private TabLayout tabLayout;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.multi_fragment_activity);

        $(R.id.multi_fragment_4_requedst_activity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MultiFragmentActivity.this, MFSecondActivity.class));
            }
        });

        tabLayout = $(R.id.multi_fragment_tab);
        viewPager = $(R.id.multi_fragment_viewPager);
        // TODO init all fragment or init only two
        viewPager.setOffscreenPageLimit(mTitles.length);
        setPagerAdapter();
    }

    private void setPagerAdapter() {
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return mTitles.length;
            }

            @Override
            public Fragment getItem(int position) {
                switch (position) {
                    case 0:
                        return new MFLetterAFragment();
                    case 1:
                        return new MFLetterBFragment();
                    case 2:
                        return new MFLetterCFragment();
                    case 3:
                        return new MFLetterDFragment();
                    default:
                        return new MFLetterAFragment();
                }
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTitles[position];
            }
        });
        tabLayout.setupWithViewPager(viewPager);
    }


} public class SingleInstanceActivity extends LaunchModeActivity {

} public class SingleTaskActivity extends LaunchModeActivity {

} public class SingleTopActivity extends LaunchModeActivity {

} public class MultiProcessActivity extends BaseBackFlowActivity implements View.OnClickListener {
    private static final String TAG = MultiProcessActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.multi_activity);

        setTitle("Multi Process View");
        $(R.id.main_btn_4_requedst_activity).setOnClickListener(this);
        $(R.id.main_btn_4_requedst_fragment).setOnClickListener(this);
        $(R.id.main_btn_4_requedst_activity_and_fragment).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_btn_4_requedst_activity: {
                Intent intent = new Intent(this, LetterAActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
            break;
            case R.id.main_btn_4_requedst_fragment: {
                Intent intent = new Intent(this, ContainerActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(ContainerActivity.PARAM_CLASSNAME, LetterAFragment.class.getName());
                startActivity(intent);
            }
            break;
            case R.id.main_btn_4_requedst_activity_and_fragment: {
                Intent intent = new Intent(this, ContainerAF1Activity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(ContainerAF1Activity.PARAM_CLASSNAME, LetterAFAFragment.class.getName());
                startActivity(intent);
            }
            break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
} public class MultiTaskActivity extends BaseBackFlowActivity implements View.OnClickListener {
    private static final String TAG = MultiTaskActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.multi_activity);

        setTitle("Multi Task View");
        $(R.id.main_btn_4_requedst_activity).setOnClickListener(this);
        $(R.id.main_btn_4_requedst_fragment).setOnClickListener(this);
        $(R.id.main_btn_4_requedst_activity_and_fragment).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_btn_4_requedst_activity: {
                Intent intent = new Intent(this, LetterAActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
            break;
            case R.id.main_btn_4_requedst_fragment: {
                Intent intent = new Intent(this, ContainerActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(ContainerActivity.PARAM_CLASSNAME, LetterAFragment.class.getName());
                startActivity(intent);
            }
            break;
            case R.id.main_btn_4_requedst_activity_and_fragment: {
                Intent intent = new Intent(this, ContainerAF1Activity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(ContainerAF1Activity.PARAM_CLASSNAME, LetterAFAFragment.class.getName());
                startActivity(intent);
            }
            break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
} public class MultiTaskAndProcessActivity extends BaseBackFlowActivity implements View.OnClickListener {
    private static final String TAG = MultiTaskAndProcessActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.multi_activity);

        setTitle("Multi Task And Process View");
        $(R.id.main_btn_4_requedst_activity).setOnClickListener(this);
        $(R.id.main_btn_4_requedst_fragment).setOnClickListener(this);
        $(R.id.main_btn_4_requedst_activity_and_fragment).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_btn_4_requedst_activity: {
                Intent intent = new Intent(this, LetterAActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
            break;
            case R.id.main_btn_4_requedst_fragment: {
                Intent intent = new Intent(this, ContainerActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(ContainerActivity.PARAM_CLASSNAME, LetterAFragment.class.getName());
                startActivity(intent);
            }
            break;
            case R.id.main_btn_4_requedst_activity_and_fragment: {
                Intent intent = new Intent(this, ContainerAF1Activity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(ContainerAF1Activity.PARAM_CLASSNAME, LetterAFAFragment.class.getName());
                startActivity(intent);
            }
            break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
} public class ContainerAF1Activity extends BaseBackFlowActivity {

    /**
     * sample: XXXFragment.class.getName()
     */
    public final static String PARAM_CLASSNAME = "className";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_container);

        BaseBackFlowFragment fragment = getFragment();
        if (fragment == null) {
//            finish();
            return;
        }

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .commitAllowingStateLoss();
    }

    private BaseBackFlowFragment getFragment() {
        String className = getIntent().getStringExtra(PARAM_CLASSNAME);
        BaseBackFlowFragment fragment = null;
        if (className != null && !className.isEmpty()) {
            try {
                fragment = (BaseBackFlowFragment) getClassLoader().loadClass(className).newInstance();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return fragment;
    }

} public class LetterAFAFragment extends BaseLetterAFFragment {

    @Override
    protected void initView() {
        super.initView();
    }
} public class ACLetterAActivity extends BaseACLetterActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

} public class LetterAFragment extends BaseLetterFragment {

    @Override
    protected void initView() {
        super.initView();
    }
} public class LetterAActivity extends BaseLetterActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

}
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import cn.ytxu.androidbackflow.BaseBackFlowActivity;
import cn.ytxu.androidbackflow.R;
import cn.ytxu.androidbackflow.sample.fragments.fragment_contains_sub_fragment.first_level.FcsfAFragment;
import cn.ytxu.androidbackflow.sample.fragments.multi_fragment.MultiFragmentActivity;
import cn.ytxu.androidbackflow.sample.launch_mode.SingleInstanceActivity;
import cn.ytxu.androidbackflow.sample.launch_mode.SingleTaskActivity;
import cn.ytxu.androidbackflow.sample.launch_mode.SingleTopActivity;
import cn.ytxu.androidbackflow.sample.multi_task_or_process.multi_process.MultiProcessActivity;
import cn.ytxu.androidbackflow.sample.multi_task_or_process.multi_task.MultiTaskActivity;
import cn.ytxu.androidbackflow.sample.multi_task_or_process.multi_task_and_process.MultiTaskAndProcessActivity;
import cn.ytxu.androidbackflow.sample.normal.request_activity_and_fragment.ContainerAF1Activity;
import cn.ytxu.androidbackflow.sample.normal.request_activity_and_fragment.letter.LetterAFAFragment;
import cn.ytxu.androidbackflow.sample.normal.request_activity_count.letter.ACLetterAActivity;
import cn.ytxu.androidbackflow.sample.normal.request_fragment.letter.LetterAFragment;
import cn.ytxu.androidbackflow.sample.normal.request_activity.letter.LetterAActivity;
package cn.ytxu.androidbackflow.sample;



public class MainActivity extends BaseBackFlowActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setTitle("Main View");

        setNormalListener();
        setMultiListener();
        setFragmentsListener();
        setLaunchModeListener();
    }

    private void setNormalListener() {
        final View.OnClickListener l = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.main_btn_4_requedst_activity:
                        startActivity(new Intent(MainActivity.this, LetterAActivity.class));
                        break;
                    case R.id.main_btn_4_requedst_fragment: {
                        Intent intent = new Intent(MainActivity.this, ContainerActivity.class);
                        intent.putExtra(ContainerActivity.PARAM_CLASSNAME, LetterAFragment.class.getName());
                        startActivity(intent);
                    }
                    break;
                    case R.id.main_btn_4_requedst_activity_and_fragment: {
                        Intent intent = new Intent(MainActivity.this, ContainerAF1Activity.class);
                        intent.putExtra(ContainerAF1Activity.PARAM_CLASSNAME, LetterAFAFragment.class.getName());
                        startActivity(intent);
                    }
                    break;
                    case R.id.main_btn_4_request_back_activity_count: {
                        startActivity(new Intent(MainActivity.this, ACLetterAActivity.class));
                    }
                    break;
                    default:
                        break;
                }
            }
        };
        $(R.id.main_btn_4_requedst_activity).setOnClickListener(l);
        $(R.id.main_btn_4_requedst_fragment).setOnClickListener(l);
        $(R.id.main_btn_4_requedst_activity_and_fragment).setOnClickListener(l);
        $(R.id.main_btn_4_request_back_activity_count).setOnClickListener(l);
    }

    private void setMultiListener() {
        final View.OnClickListener l = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.main_btn_4_multi_process: {
                        Intent intent = new Intent(MainActivity.this, MultiProcessActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                    break;
                    case R.id.main_btn_4_multi_task: {
                        Intent intent = new Intent(MainActivity.this, MultiTaskActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                    break;
                    case R.id.main_btn_4_multi_task_and_process: {
                        Intent intent = new Intent(MainActivity.this, MultiTaskAndProcessActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                    break;
                    default:
                        break;
                }
            }
        };
        $(R.id.main_btn_4_multi_process).setOnClickListener(l);
        $(R.id.main_btn_4_multi_task).setOnClickListener(l);
        $(R.id.main_btn_4_multi_task_and_process).setOnClickListener(l);
    }

    private void setFragmentsListener() {
        final View.OnClickListener l = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.main_btn_4_multi_fragment: {
                        Intent intent = new Intent(MainActivity.this, MultiFragmentActivity.class);