如何从活动中打开片段(使用ABS)

时间:2013-08-12 13:26:58

标签: android actionbarsherlock fragment slidingmenu

我正在为我的应用程序使用Action Bar Sherlock片段。我需要从活动中分段。但是,当我使用此代码进行分段时,滑动菜单无效。

活动代码

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

FragmentOpenActvity就像

public class FragmentOpenActivity extends SherlockFragmentActivity {
private Bundle i_fragment;
private String messages = null;
private String coupons = null;
private String yo = null;
private String invitation = null;
private String loyalty = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    if (savedInstanceState == null) {
        // getSupportFragmentManager().beginTransaction()
        // .add(android.R.id.content, new MessagesFragment()).commit();
        i_fragment = getIntent().getExtras();
        if (i_fragment == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, new ProfileEditorFragment())
                    .commit();
        } else {
            messages = i_fragment.getString("Message");
            coupons = i_fragment.getString("Coupon");
        }
        if (messages != null && messages.equalsIgnoreCase("Message")) {
            getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, new MessagesFragment())
                    .commit();
        } else if (coupons != null && coupons.equalsIgnoreCase("Coupon")) {
            getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, new CouponsFragment())
                    .commit();
        } else {
            getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, new ProfileEditorFragment())
                    .commit();
        }
    }
}

/**
 * @return Displaying Action Bar Button with Image
 */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return true;
}

/**
 * @return Click Events for Action Bar Buttons
 */
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case android.R.id.home:
        startActivity(new Intent(this, FragmentChangeActivity.class));
        break;
    }
    return true;
}

/**
 * @return Handling Back button
 */
public void onBackPressed() {
    startActivity(new Intent(this, FragmentChangeActivity.class));
    finish();
}
}

Please refer the following code for fragment 

public class CouponsFragment extends SherlockFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        getSherlockActivity().getSupportActionBar().setDisplayOptions(
                ActionBar.DISPLAY_SHOW_CUSTOM);
        getSherlockActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(
                true);
return inflater.inflate(R.layout.listview_refreshable, null);
    }

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

    public void onStart() {
        super.onStart();

}
}

通常滑动菜单效果很好但是当我来自FragmentOpenActivity时它没有响应。请尝试找一个解决方案

0 个答案:

没有答案