从另一个活动/ Actionbaractivity打开片段

时间:2016-07-24 17:44:34

标签: android

我想从按钮actionbaractivity打开片段。 我想从另一个活动打开片段。 请帮忙

这是我的Actionbaractivity:

public class ActionBarParentActivity extends ActionBarActivity {
    private LinkedHashSet<Integer> enableItems = new LinkedHashSet<Integer>();
    private LinkedHashSet<Integer> disableItems = new LinkedHashSet<Integer>();
    private Iterator<Integer> iter;

    public void setEnableItem(LinkedHashSet<Integer> items) {
        this.enableItems = items;
    }

    public void setDisableItem(LinkedHashSet<Integer> items) {
        this.disableItems = items;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        if (!disableItems.isEmpty()) {
            iter = disableItems.iterator();
            while (iter.hasNext()) {
                MenuItem item = menu.findItem(iter.next());
                item.setVisible(false);
            }
        }

        if (!enableItems.isEmpty()) {
            iter = enableItems.iterator();
            while (iter.hasNext()) {
                MenuItem item = menu.findItem(iter.next());
                item.setVisible(true);
            }
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case android.R.id.home:
            // NavUtils.navigateUpTo(this, new Intent(this,
            // HomeActivity.class));
            finish();
            break;

        case R.id.btn_action_upload:
            if (!Ultils.isConnectingToInternet(this)) {
                showMsg(getResources().getString(R.string.open_network));
                return false;
            }
            UserSessionManager userSession = new UserSessionManager(this);
            if (userSession.getUserSession() != null) {
                Intent intent = new Intent(this, Maps.class);
                startActivity(intent);
            } else {
                Intent intent = new Intent(this, AuthenticationActivity.class);
                startActivity(intent);
            }
            break;

        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void refreshActionBarMenu() {
        this.supportInvalidateOptionsMenu();
    }

    public void changeActionBarTitle(String title) {
        getSupportActionBar().setTitle(title);
    }

    public void showMsg(String msg) {
        Toast ts = Toast.makeText(this, msg, 5000);
        ts.show();
    }

    public void showDialog(String msg) {
        AlertDialog.Builder buidler = new AlertDialog.Builder(
                ActionBarParentActivity.this);
        buidler.setMessage(msg);
        buidler.setPositiveButton(getResources().getString(R.string.ok_label),
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method
                        // stub

                    }
                });
        AlertDialog dialog = buidler.create();
        dialog.show();
    }

}

1 个答案:

答案 0 :(得分:0)

我希望这会有所帮助。

  Fragment mFragment = new MainFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.fragment_xml, fragment).commit();