com.brandao.tictactoe.board.BoardActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.brandao.tictactoe.board.BoardActivity.java

Source

/*
 * Copyright (c) 2016 Skytrait
 *
 * 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.
 *
 * www.skytrait.com
 *
 */

package com.brandao.tictactoe.board;

import android.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.brandao.tictactoe.R;

public class BoardActivity extends AppCompatActivity {
    public final String TAG = "BoardActivity";

    private final String FRAGMENT_TAG_BOARD = "board";

    private CoordinatorLayout mCoordinatorLayout;

    private BoardFragment mBoardFragment;

    private Toolbar mToolbar;

    private SharedPreferences mPrefs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.board_activity);

        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        mCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.board_activity_coordinator_layout);

        mToolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mBoardFragment = (BoardFragment) getFragmentManager().findFragmentByTag(FRAGMENT_TAG_BOARD);
        if (mBoardFragment == null) {
            FragmentTransaction t = getFragmentManager().beginTransaction();

            mBoardFragment = BoardFragment.newInstance();
            t.replace(R.id.board_fragment_container, mBoardFragment, FRAGMENT_TAG_BOARD);
            t.commit();
        }
    }

    public CoordinatorLayout getCoordinatorLayout() {
        return mCoordinatorLayout;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home: {
            NavUtils.navigateUpFromSameTask(this);

            return true;
        }
        }

        return super.onOptionsItemSelected(item);
    }
}