com.unovo.frame.uikit.base.container.ContainerActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.unovo.frame.uikit.base.container.ContainerActivity.java

Source

/*
 * Copyright (c) 2016 ?. All Rights Reserved.
 * Use of this source code is governed by a Shanghai Unovo Information Technology Co.,Ltd license
 * that can be found in the LICENSE file in the root of the web site.
 *
 *   http://www.unovo.com.cn
 *
 * An additional intellectual property rights grant can be found
 * in the file PATENTS.  All contributing project authors may
 * be found in the AUTHORS file in the root of the source tree.
 *
 */

package com.unovo.frame.uikit.base.container;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.StyleRes;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import com.unovo.frame.R;
import com.unovo.frame.uikit.base.BaseActivity;

/**
 * Created by Aeatho on 2016/12/28 02:56
 *
 * Prject: unovo-guest-app
 * Description:
 * email: aeatho@gmail.com
 */
public class ContainerActivity extends BaseActivity {
    private static final String FRAGMENT_TAG = "fragmentTG";
    private static final String CONTENTVIEW_ID = "containerID";
    private static final String CONTENTVIEW_THEME = "containerTheme";
    private static final String FRAGMENT_NAME = "fragmentName";
    private static final String FRAGMENT_ARGS = "fragmentArgs";

    private @LayoutRes int contentViewId;
    private @StyleRes int contentViewTheme;

    /**
     * ??
     */
    public static void launch(Activity activity, Class<? extends Fragment> clazz) {
        launch(activity, clazz, null);
    }

    public static void launch(Fragment fragment, Class<? extends Fragment> clazz) {
        launch(fragment, clazz);
    }

    public static void launch(Activity activity, Class<? extends Fragment> clazz, Bundle args) {
        Intent intent = new Intent(activity, ContainerActivity.class);
        intent.putExtra(FRAGMENT_NAME, clazz.getName());
        if (args != null)
            intent.putExtra(FRAGMENT_ARGS, args);
        activity.startActivity(intent);
    }

    public static void launch4Result(Fragment fragment, Class<? extends Fragment> clazz, Bundle args,
            int requestCode) {
        if (fragment.getActivity() == null)
            return;
        Activity activity = fragment.getActivity();

        Intent intent = new Intent(activity, ContainerActivity.class);
        intent.putExtra(FRAGMENT_NAME, clazz.getName());
        if (args != null)
            intent.putExtra(FRAGMENT_ARGS, args);
        fragment.startActivityForResult(intent, requestCode);
    }

    public static void launch4Result(Activity activity, Class<? extends Fragment> clazz, Bundle args,
            int requestCode) {
        Intent intent = new Intent(activity, ContainerActivity.class);
        intent.putExtra(FRAGMENT_NAME, clazz.getName());
        if (args != null)
            intent.putExtra(FRAGMENT_ARGS, args);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected int getLayoutId() {
        return contentViewId;
    }

    @SuppressWarnings("all")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        contentViewId = savedInstanceState == null ? R.layout.activity_container
                : savedInstanceState.getInt(CONTENTVIEW_ID);
        contentViewTheme = savedInstanceState == null ? -1 : savedInstanceState.getInt(CONTENTVIEW_THEME);

        Fragment fragment = null;
        if (savedInstanceState == null) {
            try {
                String className = getIntent().getStringExtra(FRAGMENT_NAME);
                if (TextUtils.isEmpty(className)) {
                    finish();
                    return;
                }

                Bundle values = getIntent().getParcelableExtra(FRAGMENT_ARGS);
                Class clazz = Class.forName(className);
                fragment = (Fragment) clazz.newInstance();

                if (values != null) {
                    // ?Fragment
                    fragment.setArguments(values);
                }
                if (fragment instanceof FragmentSettingsCallBack) {
                    // ?ActivitycontentView
                    //int tempLayoutId = ((FragmentSettingsCallBack) fragment).setContainerLayoutID();
                    //if (tempLayoutId != -1) contentViewId = tempLayoutId;

                    int tempTheme = ((FragmentSettingsCallBack) fragment).setContainerTheme();
                    if (tempTheme != -1)
                        contentViewTheme = tempTheme;
                }
            } catch (Exception ignored) {
                super.onCreate(savedInstanceState);
                finish();
                return;
            }
        }
        // 
        if (contentViewTheme != -1) {
            setTheme(contentViewTheme);
        }
        super.onCreate(savedInstanceState);

        if (fragment != null) {
            getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer, fragment, FRAGMENT_TAG)
                    .commitAllowingStateLoss();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(CONTENTVIEW_ID, contentViewId);
        outState.putInt(CONTENTVIEW_THEME, contentViewTheme);
    }
}