Java tutorial
/* * Copyright (C) 2016 AdvancingPainters (https://github.com/AdvancingPainters). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.ap.github.ui.activitys; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.StringRes; import android.support.v4.app.Fragment; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; import android.widget.TextView; import com.ap.github.R; import com.ap.github.base.BaseActivity; import com.ap.github.utils.Constants; import butterknife.Bind; import butterknife.ButterKnife; import butterknife.OnClick; /** * simple fragment activity * Created by Spencer on 4/1/16. */ public class SimpleFragmentActivity extends BaseActivity { @Bind(R.id.leftButton) Button mLeftButton; @Bind(R.id.titleTextView) TextView mTitleTextView; @Bind(R.id.rightButton) Button mRightButton; @Bind(R.id.containerFrameLayout) FrameLayout mContainerFrameLayout; OnLeftButtonClickListener mOnLeftButtonClickListener; OnRightButtonClickListener mOnRightButtonClickListener; public static Intent newIntent(Context context, Class fragmentClass) { return newIntent(context, null, fragmentClass, null); } public static Intent newIntent(Context context, Class fragmentClass, Bundle fragmentArguments) { return newIntent(context, null, fragmentClass, fragmentArguments); } public static Intent newIntent(Context context, @StringRes int titleResId, Class fragmentClass) { return newIntent(context, context.getString(titleResId), fragmentClass, null); } public static Intent newIntent(Context context, String title, Class fragmentClass) { return newIntent(context, title, fragmentClass, null); } public static Intent newIntent(@NonNull Context context, String title, @NonNull Class fragmentClass, Bundle fragmentArguments) { Intent intent = new Intent(context, SimpleFragmentActivity.class); intent.putExtra(Constants.Extras.FRAGMENT_CLASS_NAME, fragmentClass.getName()); if (!TextUtils.isEmpty(title)) { intent.putExtra(Constants.Extras.FRAGMENT_TITLE, title); } if (fragmentArguments != null) { intent.putExtra(Constants.Extras.FRAGMENT_ARGUMENTS, fragmentArguments); } return intent; } @Override protected boolean needRegisterEventBus() { return false; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_simple_fragment); ButterKnife.bind(this); initArguments(); } public void initArguments() { boolean hasFragment = getIntent().hasExtra(Constants.Extras.FRAGMENT_CLASS_NAME); if (!hasFragment) { throw new IllegalArgumentException("Fragment name is null."); } String fragmentName = getIntent().getStringExtra(Constants.Extras.FRAGMENT_CLASS_NAME); Fragment fragment = Fragment.instantiate(this, fragmentName); if (fragment == null) { throw new IllegalArgumentException("Simple fragment is null."); } boolean hasTitle = getIntent().hasExtra(Constants.Extras.FRAGMENT_TITLE); if (hasTitle) { String mTitle = getIntent().getStringExtra(Constants.Extras.FRAGMENT_TITLE); setTitle(mTitle); } boolean hasFragmentArguments = getIntent().hasExtra(Constants.Extras.FRAGMENT_ARGUMENTS); if (hasFragmentArguments) { Bundle fragmentArguments = getIntent().getBundleExtra(Constants.Extras.FRAGMENT_ARGUMENTS); fragment.setArguments(fragmentArguments); } setContentFragment(fragment); } public void setTitle(String title) { if (!TextUtils.isEmpty(title)) { mTitleTextView.setText(title); } } public void setContentFragment(Fragment fragment) { getSupportFragmentManager().beginTransaction() .add(R.id.containerFrameLayout, fragment, fragment.getClass().getSimpleName()) .commitAllowingStateLoss(); } public Button getLeftButton() { return mLeftButton; } public Button getRightButton() { return mRightButton; } @OnClick(R.id.leftButton) public void leftButtonClick(View leftButton) { if (mOnLeftButtonClickListener != null) { mOnLeftButtonClickListener.onLeftButtonClickListener(leftButton); } else { finish(); } } @OnClick(R.id.rightButton) public void rightButtonClick(View rightButton) { if (mOnRightButtonClickListener != null) { mOnRightButtonClickListener.onRightButtonClickListener(rightButton); } } public void setOnLeftButtonClickListener(OnLeftButtonClickListener mOnLeftButtonClickListener) { if (mLeftButton != null) { this.mOnLeftButtonClickListener = mOnLeftButtonClickListener; } } public void setOnRightButtonClickListener(OnRightButtonClickListener mOnRightButtonClickListener) { if (mRightButton != null) { mRightButton.setVisibility(View.VISIBLE); this.mOnRightButtonClickListener = mOnRightButtonClickListener; } } public interface OnLeftButtonClickListener { void onLeftButtonClickListener(View leftButton); } public interface OnRightButtonClickListener { void onRightButtonClickListener(View rightButton); } }