Skip to content

Instantly share code, notes, and snippets.

@akexorcist
Last active August 29, 2015 14:19
Show Gist options
  • Save akexorcist/36a10c6d39276106a8e7 to your computer and use it in GitHub Desktop.
Save akexorcist/36a10c6d39276106a8e7 to your computer and use it in GitHub Desktop.
ตัวอย่างการ Detect Current Fragment เพื่อซ่อน View บางอย่างใน Activity
package com.akexorcist.fragmenttest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends ActionBarActivity implements View.OnClickListener, FragmentManager.OnBackStackChangedListener {
private Button btnP1, btnP2, btnP3, btnP4;
private LinearLayout layoutTitle;
private FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layoutTitle = (LinearLayout) findViewById(R.id.layout_title);
btnP1 = (Button) findViewById(R.id.btn_p1);
btnP2 = (Button) findViewById(R.id.btn_p2);
btnP3 = (Button) findViewById(R.id.btn_p3);
btnP4 = (Button) findViewById(R.id.btn_p4);
btnP1.setOnClickListener(this);
btnP2.setOnClickListener(this);
btnP3.setOnClickListener(this);
btnP4.setOnClickListener(this);
fragmentManager = getSupportFragmentManager();
fragmentManager.addOnBackStackChangedListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_p1 :
replaceFragment(OneFragment.getInstance());
break;
case R.id.btn_p2 :
replaceFragment(TwoFragment.getInstance());
break;
case R.id.btn_p3 :
replaceFragment(ThreeFragment.getInstance());
break;
case R.id.btn_p4 :
replaceFragment(FourFragment.getInstance());
break;
}
}
@Override
public void onBackStackChanged() {
Fragment fragment = getActiveFragment();
if(fragment != null) {
String fragmentName = fragment.getClass().getSimpleName();
if (OneFragment.class.getSimpleName().equals(fragmentName)) {
layoutTitle.setVisibility(View.VISIBLE);
} else if (TwoFragment.class.getSimpleName().equals(fragmentName)) {
layoutTitle.setVisibility(View.VISIBLE);
} else if (ThreeFragment.class.getSimpleName().equals(fragmentName)) {
layoutTitle.setVisibility(View.GONE);
} else if (FourFragment.class.getSimpleName().equals(fragmentName)) {
layoutTitle.setVisibility(View.VISIBLE);
}
}
}
private void replaceFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment, fragment.getClass().getSimpleName());
transaction.addToBackStack(fragment.getClass().getSimpleName());
transaction.commit();
}
private Fragment getActiveFragment() {
if (fragmentManager.getBackStackEntryCount() == 0) {
return null;
}
String tag = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount() - 1).getName();
return fragmentManager.findFragmentByTag(tag);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment