Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.vyda.stream.fragments;
- import android.annotation.TargetApi;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Build;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentManager;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.TextView;
- import com.vyda.stream.R;
- import com.vyda.stream.StreamApplication;
- import com.vyda.stream.activities.BaseActivity;
- import com.vyda.stream.backend.RestService;
- import com.vyda.stream.helpers.BusHelper;
- import com.vyda.stream.helpers.Global;
- import javax.inject.Inject;
- import butterknife.ButterKnife;
- import butterknife.InjectView;
- import butterknife.Optional;
- public abstract class BaseFragment extends Fragment {
- @Inject
- protected RestService restService;
- @Optional
- @InjectView(R.id.popover_header_text)
- protected TextView tvHeader;
- protected int backgroundColor = 0;
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- ((StreamApplication)getActivity().getApplication()).getComponent().inject(this);
- onViewsFragment(view, savedInstanceState);
- }
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- int contentView = onFragmentContentView();
- if(contentView == 0)
- throw new IllegalArgumentException("onFragmentContentView must be not equal 0");
- View layout = inflater.inflate(contentView, container, false);
- ButterKnife.inject(this, layout);
- onCreateFragmentView(layout, container, savedInstanceState);
- return layout;
- }
- @Override
- public void onResume() {
- super.onResume();
- BusHelper.getInstance().register(this);
- }
- @Override
- public void onPause() {
- super.onPause();
- BusHelper.getInstance().unregister(this);
- }
- @Override
- public void onDestroyView() {
- super.onDestroyView();
- ButterKnife.reset(this);
- }
- @Override
- public Animation onCreateAnimation(int transit, final boolean enter, int nextAnim) {
- return AnimationUtils.loadAnimation(getActivity(), enter ? R.anim.fade_in : R.anim.fade_out);
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if(requestCode == Global.RequestCode.RELOGIN && resultCode == Activity.RESULT_OK) {
- updateViewAfterLogin();
- }
- }
- @Override
- @TargetApi(Build.VERSION_CODES.M)
- public void onAttach(Context context) {
- super.onAttach(context);
- onAttachFragment(context);
- }
- @Override
- @SuppressWarnings("deprecation")
- public void onAttach(Activity activty) {
- super.onAttach(activty);
- onAttachFragment(activty);
- }
- public boolean onBackPressed() {
- return false;
- }
- /**
- * Use this method instead #onAttach(Activity)
- * and #onAttach(Context)
- * @param context
- */
- protected void onAttachFragment(Context context) {
- }
- protected abstract int onFragmentContentView();
- /**
- * Override this method instead onCreateView
- */
- protected abstract void onCreateFragmentView(View v, ViewGroup container, Bundle savedInstanceState);
- /**
- * Override this method instead onViewCreated
- */
- protected abstract void onViewsFragment(View view, Bundle savedInstanceState);
- public BaseFragment setBackground(int backgroundColor) {
- this.backgroundColor = backgroundColor;
- return this;
- }
- public void runActivity(Class<?> className) {
- ((BaseActivity) getActivity()).runActivity(className);
- }
- public void runActivity(Class<?> className, Bundle bundle) {
- ((BaseActivity) getActivity()).runActivity(className, bundle);
- }
- public void runActivityForResult(Class<?> className, Bundle bundle, int request) {
- ((BaseActivity) getActivity()).runActivityForResult(className, bundle, request);
- }
- public void replaceFragment(Fragment fragment) {
- ((BaseActivity) getActivity()).replaceFragment(fragment);
- }
- protected void showFragment(Fragment fragment, String tag) {
- ((BaseActivity) getActivity()).showFragment(fragment, tag);
- }
- protected void showFragment(Fragment fragment, String tag, int id) {
- ((BaseActivity) getActivity()).showFragment(fragment, tag, id);
- }
- protected void showFragmentWithoutStack(Fragment fragment, String tag) {
- ((BaseActivity) getActivity()).showFragmentWithoutStack(fragment, tag);
- }
- protected void showFragmentWithoutStack(Fragment fragment, String tag, int id) {
- ((BaseActivity) getActivity()).showFragmentWithoutStack(fragment, tag, id);
- }
- protected void showChildFragment(FragmentManager fragmentManager, Fragment fragment, String tag, int id) {
- ((BaseActivity) getActivity()).showChildFragment(fragmentManager, fragment, tag, id);
- }
- public void showSnackbar(Fragment fragment, String tag) {
- ((BaseActivity) getActivity()).showSnackbar(fragment, tag);
- }
- public void updateViewAfterLogin() {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement