I have two fragments (Fragment1 and Fragment2), created ActionBar navigation with custom TabListener Class. Each tab's contant is one of my fragments. But now I have a problem - I can't access / get fragment's instances.
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
getSupportActionBar().setNavigationMode( getSupportActionBar().NAVIGATION_MODE_TABS );
Tab tab = getSupportActionBar().newTab()
.setText( R.string.frag1 )
.setTabListener(
new MyTabListener( this,
Fragment1.class.getName() ) );
getSupportActionBar().addTab( tab );
tab = getSupportActionBar().newTab()
.setText( R.string.frag2 )
.setTabListener(
new MyTabListener( this,
Fragment2.class.getName() ) );
getSupportActionBar().addTab( tab );
Fragment1 f1 = (Fragment1) getSupportFragmentManager().findFragmentByTag("FRAG1");
if(f1 != null) {
Toast.makeText(this, "It's not empty", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(this, "Photo Intent Goes Here", Toast.LENGTH_SHORT).show();
if(data != null) {
Toast.makeText(this, "It's not empty", Toast.LENGTH_SHORT).show();
if (resultCode == RESULT_OK) {
}
} else {
Toast.makeText(this, "It's empty", Toast.LENGTH_SHORT).show();
}
}
private class MyTabListener implements TabListener
{
private SherlockFragment mFragment;
private final Activity mActivity;
private final String mFragName;
public MyTabListener( Activity activity,
String fragName )
{
mActivity = activity;
mFragName = fragName;
}
@Override
public void onTabSelected(Tab tab,
android.support.v4.app.FragmentTransaction ft) {
mFragment = (SherlockFragment) SherlockFragment.instantiate( mActivity,
mFragName );
if( mFragment instanceof Fragment1 ) {
Fragment1 f1temp = (Fragment1) mFragment;
//Toast.makeText(mActivity, "Frag1", Toast.LENGTH_SHORT).show();
ft.add( android.R.id.content, mFragment,"FRAG1");
}
else {
ft.add( android.R.id.content, mFragment,"ELSE");
}
//Toast.makeText(mActivity, mFragName, Toast.LENGTH_SHORT).show();
}
@Override
public void onTabUnselected(Tab tab,
android.support.v4.app.FragmentTransaction ft) {
ft.remove( mFragment );
mFragment = null;
}
public SherlockFragment getCurrentFragment() {
return mFragment;
}
@Override
public void onTabReselected(Tab tab,
android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
I can't use getSupportFragmentManager().findFragmentById( id )
, because I have no id also I have tried getSupportFragmentManager().findFragmentByTag( tag )
but it didn't work either because I only call add()
method FragmentTransaction
class not replace()
Is it any way to get current tab fragment instance so I can call fragment's methods?
I found a solution. I tried to get instance of fragment in onActivityResult()
method and that actually worked.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(this, "Photo Intent Goes Here", Toast.LENGTH_SHORT).show();
if(data != null) {
Toast.makeText(this, "It's not empty", Toast.LENGTH_SHORT).show();
if (resultCode == RESULT_OK) {
Fragment1 f1 = (Fragment1) getSupportFragmentManager().findFragmentByTag("FRAG1");
f1.onCameraResult(requestCode,resultCode,data); // Your method of the fragment
}
} else {
Toast.makeText(this, "It's empty", Toast.LENGTH_SHORT).show();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With