package com.galois.qrstream;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.galois.qrstream.lib.ReceiveFragment;
import com.galois.qrstream.lib.TransmitFragment;

public class MainActivity extends Activity {

    private FragmentManager fragmentManager;
    public Fragment receiveFragment; // accessed via unittest
    public Fragment transmitFragment; // accessed via unittest

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentManager = getFragmentManager();
        receiveFragment = new ReceiveFragment();
        transmitFragment = new TransmitFragment();

        if (savedInstanceState == null) {
            fragmentManager.beginTransaction()
                    .add(R.id.container, receiveFragment)
                    .commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        if (id == R.id.action_receive) {
            fragmentManager.beginTransaction()
                    .replace(R.id.container, receiveFragment)
                    .commit();
            return true;
        }
        if (id == R.id.action_transmit) {
            fragmentManager.beginTransaction()
                    .replace(R.id.container, transmitFragment)
                    .commit();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }


}

@@ -113,24 +113,55 @@
 entManager;%0A
+import android.content.Intent;%0A
 import andro
@@ -989,32 +989,188 @@
  == null) %7B%0A    
+        Intent startingIntent = getIntent();%0A            showFragment(receiveFragment);%0A        %7D%0A    %7D%0A%0A    private void showFragment(Fragment fragment) %7B%0A
         fragment
@@ -1216,16 +1216,16 @@
     
-    .add
+.replace
 (R.i
@@ -1233,32 +1233,25 @@
 .container, 
-receiveF
+f
 ragment)%0A   
@@ -1255,36 +1255,32 @@
                 
-   
- 
 .commit();%0A     
@@ -1274,26 +1274,16 @@
 mmit();%0A
-        %7D%0A
     %7D%0A%0A 
@@ -1966,133 +1966,36 @@
     
-f
+showF
 ragment
-Manager.beginTransaction()%0A                    .replace(R.id.container, receiveFragment)%0A                    .commit(
+(receiveFragment
 );%0A 
@@ -2086,134 +2086,37 @@
     
-f
+showF
 ragment
-Manager.beginTransaction()%0A                    .replace(R.id.container, transmitFragment)%0A                    .commit(
+(transmitFragment
 );%0A 

make fragment swapping a bit simpler