package com.kevoroid.andhackernews.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.kevoroid.andhackernews.R;
import com.kevoroid.andhackernews.adapters.StoryListAdapter;

public class MainActivity extends BaseActivity implements StoryListAdapter.StoryListAdapterInterface {

    private Fragment mStoryListFragment;
    private Fragment mStoryDetailFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (mStoryListFragment == null) {
            mStoryListFragment = StoryListFragment.newInstance();
            addFragment(mStoryListFragment);
        } else {
            addFragment(mStoryListFragment);
        }
    }

    protected void addFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.main_activity_fragment_container, fragment);
        fragmentTransaction.commit();
    }

    protected void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.main_activity_fragment_container, fragment).addToBackStack(fragment.getTag());
        fragmentTransaction.commit();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (mStoryListFragment.isVisible()) {
            finish();
        }
    }

    @Override
    public void onItemClick() {
        if (mStoryDetailFragment == null) {
            mStoryDetailFragment = StoryDetailFragment.newInstance();
            replaceFragment(mStoryDetailFragment);
        } else {
            replaceFragment(mStoryDetailFragment);
        }
    }
}

@@ -103,55 +103,8 @@
 nt;%0A
-import android.support.v4.app.FragmentManager;%0A
 impo
@@ -935,192 +935,58 @@
     
-FragmentManager fragmentManager = getSupportFragmentManager();%0A        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();%0A        fragmentTransaction.replace
+getSupportFragmentManager().beginTransaction().add
 (R.i
@@ -1034,37 +1034,42 @@
 ent)
-;%0A        fragmentTransaction
+.addToBackStack(fragment.getTag())
 .com
@@ -1150,184 +1150,145 @@
     
-FragmentManager fragmentManager = getSupportFragmentManager();%0A        FragmentTransaction fragmen
+getSupportFragmentManager().beginTransaction()%0A                .se
 tTrans
-ac
+i
 tion
- = f
+(F
 ragment
-Manager.beginTransaction();%0A        fragmentTransaction
+Transaction.TRANSIT_FRAGMENT_OPEN)%0A                
 .rep
@@ -1374,17 +1374,16 @@
 etTag())
-;
 %0A       
@@ -1387,27 +1387,16 @@
     
-fragmentTransaction
+        
 .com
@@ -1461,39 +1461,8 @@
 ) %7B%0A
-        super.onBackPressed();%0A
     
@@ -1538,248 +1538,231 @@
    %7D
-%0A    %7D%0A%0A    @Override%0A    public void onItemClick() %7B%0A        if (mStoryDetailFragment == null) %7B%0A            mStoryDetailFragment = StoryDetailFragment.newInstance();%0A            replaceFragment(mStoryDetailFragment);%0A        %7D else %7B%0A    
+ else %7B%0A            super.onBackPressed();%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void onItemClick(String title, int commentsCount) %7B%0A        mStoryDetailFragment = StoryDetailFragment.newInstance(title, commentsCount);%0A
     
@@ -1800,30 +1800,20 @@
 lFragment);%0A
-        %7D%0A
     %7D%0A%7D%0A

simplify main activity