Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

android - How to go to fragment from activity

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);

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...