7

I am using ActionBarSherlock, I am not able to go to class that extends SherlockFragment from activity

I need to move from Activity to fragment class

Here is my Activity class

Intent notificationIntent = new Intent(context,MessagesFragment.class);

And the Fragment class is like

public class MessagesFragment extends SherlockFragment implements
    OnItemClickListener {

// Layout parameters declaration
private PullToRefreshListView lv_messages;
private ImageView iv_no_data;
private LinearLayout ll_bg;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    getSherlockActivity().getSupportActionBar().setDisplayOptions(
            ActionBar.DISPLAY_SHOW_CUSTOM);
    getSherlockActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(
            true);
    getSherlockActivity().getSupportActionBar().setHomeButtonEnabled(true);
    getSherlockActivity().getSupportActionBar().setDisplayShowHomeEnabled(
            true);
    getSherlockActivity().getSupportActionBar().setCustomView(
            R.layout.header);
    getSherlockActivity().getSupportActionBar().setBackgroundDrawable(
            new ColorDrawable(Color.parseColor("#009fe3")));
    TextView txt = (TextView) getActivity().findViewById(
            R.id.tv_title_header);
    Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
            "georgia.ttf");
    txt.setText("MESSAGES");
    txt.setTypeface(font);
    return inflater.inflate(R.layout.listview_refreshable, null);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}
.
.
.
.
}

If I use switchfragment method it shows lots of errors in FragmentChangeActivity

private void switchFragment(Fragment fragment) {
    if (getActivity() == null)
        return;

    if (getActivity() instanceof FragmentChangeActivity) {
        FragmentChangeActivity fca = (FragmentChangeActivity) getActivity();
        fca.switchContent(fragment);

    }
}
sarabu
  • 503
  • 1
  • 8
  • 20

4 Answers4

16

You need to created a class that extends FragmentActivity and start yourfragment there

public class MessagesFragmentActivity extends SherlockFragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null){
            getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, new MessagesFragment ()).commit();}
    }
}

Your fragment constructor.

public YourFragment() {
}

then from you calling activity, start your fragment activity in the normal way

Intent i = new Intent(YourActivity.this,MessagesFragment.class);
startActivity(i);
124697
  • 22,097
  • 68
  • 188
  • 315
1

Use FragmentTransaction to go to any Fragment you want. If you have several Fragments, this method will switch between them.

Here is direction:

public enum FragmentsAvailable {    
HISTORY
 }


public class MyActivity extends FragmentActivity
 ...

 private void changeFragment(Fragment newFragment, FragmentsAvailable newFragmentType, boolean withoutAnimation) {


    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();


    try {
        getSupportFragmentManager().popBackStackImmediate(newFragmentType.toString(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
    } catch (java.lang.IllegalStateException e) {

    }

    transaction.addToBackStack(newFragmentType.toString());
    transaction.replace(R.id.fragmentContainer, newFragment);
    transaction.commitAllowingStateLoss();
    getSupportFragmentManager().executePendingTransactions();


}
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
0

You can not do that. Because every Fragment was contained in a Activity, So you can just jump to a Activity which contains that Fragment.

JaredLuo
  • 481
  • 3
  • 9
0

A fragment is Attached to an activity, you can Add a fragment or Replace a fragment with FragmentTransition. Note that a fragment need an activity to exist !

You don't go from activity to fragment... But if you are in an activity that contain a fragment you can open a new activity on top of the first.

An-droid
  • 6,433
  • 9
  • 48
  • 93
  • Your assumption is wrong. We can move from activity to fragment by the code below – sarabu Aug 02 '13 at 11:20
  • Off course you are right, i'm just giving an advice about the MAIN use of fragments. You should admit that this is not common use. If you take a look at official google best practices, fragments are clearly meant to adapt UI between headset, tablets, and orientation changes... Down voting an answer for that kind of reason is idiotic :/ – An-droid Aug 02 '13 at 12:30