片段活动标题栏上的后退按钮意图

时间:2014-01-14 08:59:52

标签: android android-layout android-intent android-fragments

Fragment Activity Class如何意图转到Fragment Class

我发布的以下代码用于活动类,其中有back button on the title bar.

现在我想做的是同样但我想要implement it in an fragment class

我在我的活动类中试过这个..

   ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

以及

public boolean onOptionsItemSelected(MenuItem item){
    Intent myIntent = new Intent(getApplicationContext(), LoginActivity.class);
    startActivityForResult(myIntent, 0);
    finish();
    return true;

}

1 个答案:

答案 0 :(得分:0)

试试这个

@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {

        super.onOptionsItemSelected(item);
        int id = item.getItemId();

        switch(id)
        {

        case android.R.id.home:
            // if (page == PAGE_CHILD) finish();
        Intent myIntent = new Intent(getApplicationContext(), LoginActivity.class);
        startActivityForResult(myIntent, 0);
        finish();
            break;

        default:
            break;
        }
        return true;
    }

对于片段,你可以使用

FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.popBackStack();

在意图的地方。

或替换片段

        LoginScreen firstFragment = new LoginScreen();

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.article_fragment, firstFragment).commit();