Skip to content

Instantly share code, notes, and snippets.

@kalvian1060
Created May 20, 2017 08:54
Show Gist options
  • Save kalvian1060/275474cca1a8ed1b290493bcacad0f31 to your computer and use it in GitHub Desktop.
Save kalvian1060/275474cca1a8ed1b290493bcacad0f31 to your computer and use it in GitHub Desktop.
Hasil Akhir MainActivity
package id.kalviansofian.belajarfragment;
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;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button addFragment, replaceFragement, removeFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addFragment = (Button) findViewById(R.id.addFragment);
replaceFragement = (Button) findViewById(R.id.replaceFragment);
removeFragment = (Button) findViewById(R.id.removeFragment);
//set listener
addFragment.setOnClickListener(this);
replaceFragement.setOnClickListener(this);
removeFragment.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view == addFragment) {
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.add(R.id.action_container,new ContohFragment1()).commit(); //dan tambahkan disini
}
if (view == replaceFragement) {
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.action_container,new ContohFragment2()).commit();
}
if (view == removeFragment) {
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.action_container);
linearLayout.removeAllViews();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment