package net.ddns.mahaljsp.ch14_01_fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private FragmentManager manager;
    private int picture;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        manager=getSupportFragmentManager();
        FragmentTransaction transaction=manager.beginTransaction();
        transaction.add(R.id.container, new FragmentA(), "FragmentA");
        transaction.commit();
    }
    public void btn1_click(View view){
        FragmentTransaction transaction=manager.beginTransaction();
        transaction.replace(R.id.container, new FragmentA(), "FragmentA");
        transaction.commit();
    }
    public void btn2_click(View view){
        FragmentTransaction transaction=manager.beginTransaction();
        transaction.replace(R.id.container, new FragmentB(), "FragmentB");
        transaction.commit();
    }
    public void btn3_click(View view){
        Fragment f=manager.findFragmentByTag("FragmentA");
        if(f!=null) {
            FragmentTransaction transaction=manager.beginTransaction();
            transaction.remove(f);
            transaction.commit();
        }
    }
    public void btn4_click(View view){
        Fragment f=manager.findFragmentByTag("FragmentB");
        if(f!=null) {
            FragmentTransaction transaction=manager.beginTransaction();
            transaction.remove(f);
            transaction.commit();
        }
    }
    public void showPicture(int p){
        picture=p;
        FragmentTransaction transaction=manager.beginTransaction();
        transaction.replace(R.id.container, new FragmentB(), "FragmentB");
        transaction.commit();
    }
    public int getPicture(){return picture;}
}
