从FragmentActivity返回到之前的Fragment

时间:2014-06-02 07:52:37

标签: android android-fragments

我有一个ViewPager,里面有3个片段。

在片段1中,有一个按钮,当点击时会打开FragmentActivity

FragmentActivity内,还有一个按钮,当点击时,会将一个字符串发送到之前调用的片段并更新其UI。

我的问题是如何返回上一个片段来更新其UI?

在包含3个碎片的MainActivity中:

public class MainActivity extends ActionBarActivity implements
        ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.setOffscreenPageLimit(3); // doesn't reload menu items

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this)
            );
        }

    }
}

PagerAdapter:

public class SectionsPagerAdapter extends FragmentPagerAdapter {


    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return new FirstFragment(); 
            case 1:
                return new SecondFragment();
            case 2:
                return new ThirdFragment();
        }

        return null;
    }

}

FragmentActivity:

public static class TheFragmentActivity extends FragmentActivity {

    private TextView text;
    private Button btnToHome;

    public TheFragmentActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_login);

        text = (TextView) findViewById(R.id.txt);
        text.setText(getIntent().getStringExtra("something")); // should return 'shenanigans'
        btnToHome = (Button) findViewById(R.id.btnToHome);
        btnToHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // when clicked, return to FirstFragment
                // updates FirstFragment UI with a String from this Activity
            }
        });

    }
}

调用TheFragmentActivity

的FirstFragment
public static class FirstFragment extends Fragment {

    private Button btnNew;

    public FirstFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_placeholder, container, false);
        btnNew = (Button) rootView.findViewById(R.id.btnNew);
        btnNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(getActivity(), TheFragmentActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                i.putExtra("something", "shenanigans");
                startActivity(i);
            }
        });

        return rootView;
    }
}

2 个答案:

答案 0 :(得分:0)

检查this&amp;试试这个

Fragment duedateFrag = new DuedateFrag(true);
FragmentTransaction ft  = getFragmentManager().beginTransaction();
ft.replace(R.id.main_tasklistdetail_lay, duedateFrag);
ft.addToBackStack(null);
ft.commit();

希望这有帮助。

答案 1 :(得分:0)

我通过使用覆盖onActivityResult来解决它我想要更新的片段。

FragmentActivity onClick中,我将结果设置为我之前想要的片段中捕获:

Intent i = getIntent();
i.putExtra("fromLoggedIn", "some string");
setResult(RESULT_OK, i);
finish();

FirstFragment我凌驾于

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == RESULT_OK) {
            Log.d(PlaceholderFragment.class.getSimpleName(), "data= "+data.getStringExtra("fromLoggedIn")); // returns "some string"
        }
    }