com.lillicoder.newsblurry.stories.StoriesActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.lillicoder.newsblurry.stories.StoriesActivity.java

Source

/**
 * Copyright 2012 Scott Weeden-Moody
 *
 * 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.lillicoder.newsblurry.stories;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.MenuItem;

import com.lillicoder.actionbarcompatplus.ActionBarHelper;
import com.lillicoder.newsblurry.BaseActivity;
import com.lillicoder.newsblurry.R;
import com.lillicoder.newsblurry.feeds.Feed;

/**
 * Activity that displays stories for a single feed.
 * @author lillicoder
 */
public class StoriesActivity extends BaseActivity {

    private static final String EXCEPTION_NO_FEED_IN_LAUNCHING_INTENT = "There was no feed in the launching intent, cannot displays stories.";

    public static final String INTENT_FEED_TO_DISPLAY = "intent_feedToDisplay";

    private StoriesListFragment _storiesListFragment;

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

        // Get feed to display from launching intent and configure
        // stories list.
        Intent intent = this.getIntent();
        Feed feed = this.getFeedFromIntent(intent);
        if (feed != null)
            this._storiesListFragment.setFeed(feed);
        else
            throw new IllegalStateException(EXCEPTION_NO_FEED_IN_LAUNCHING_INTENT);
    }

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

        ActionBarHelper helper = this.getActionBarHelper();
        helper.setDisplayHomeAsUpEnabled(true);

        // Check for the feed in the launching intent. Set the title
        // of in the ActionBar to that feed's name.
        Intent intent = this.getIntent();
        Feed feed = this.getFeedFromIntent(intent);
        if (feed != null)
            helper.setTitle(feed.getName());
    }

    @Override
    protected void onInitializeChildReferences() {
        FragmentManager manager = this.getSupportFragmentManager();
        this._storiesListFragment = (StoriesListFragment) manager
                .findFragmentById(R.id.StoriesActivity_storiesListFragment);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if (android.R.id.home == itemId) {
            this.finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * Gets the {@link Feed} contained in the given {@link Intent} under the
     * {@link #INTENT_FEED_TO_DISPLAY} key.
     * @param intent {@link Intent} to get the feed from.
     * @return {@link Feed} contained in the given intent,
     *          <code>null</code> if there was no feed found.
     */
    private Feed getFeedFromIntent(Intent intent) {
        Feed feed = null;

        if (intent != null && intent.hasExtra(INTENT_FEED_TO_DISPLAY))
            feed = (Feed) intent.getSerializableExtra(INTENT_FEED_TO_DISPLAY);

        return feed;
    }

}