从Fragment调用FragmentActivity

时间:2014-02-17 17:39:29

标签: android android-fragments android-fragmentactivity

我可以从片段中调用片段活动吗?

如果我可以打电话,怎么办呢?

否则,我该如何解决这个问题?

在我的代码中,我想从“FragmentMain”片段调用FragmentWizard.class,但这不能编译。

以下是我的代码:

public class FragmentMain extends Fragment {
TextView textView;
Button x;

public FragmentMain() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_main, null);

    textView = (TextView) view.findViewById(R.id.fragment_main_textview);
    x = (Button) view.findViewById(R.id.deneme);
    x.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent firstpage= new Intent(getActivity(),FragmentWizard.class);
            startActivity(firstpage);
        }
    });


    return view;
}
}

FragmentWizard.java

public class FragmentWizard extends FragmentActivity implements
    PageFragmentCallbacks, ReviewFragment.Callbacks, ModelCallbacks {
private ViewPager mPager;
private MyPagerAdapter mPagerAdapter;

private boolean mEditingAfterReview;

private AbstractWizardModel mWizardModel = new SandwichWizardModel(this);

private boolean mConsumePageSelectedEvent;

private Button mNextButton;
private Button mPrevButton;

private List<Page> mCurrentPageSequence;
private StepPagerStrip mStepPagerStrip;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState != null) {
        mWizardModel.load(savedInstanceState.getBundle("model"));
    }

    mWizardModel.registerListener(this);

    mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mPagerAdapter);
    mStepPagerStrip = (StepPagerStrip) findViewById(R.id.strip);
    mStepPagerStrip
            .setOnPageSelectedListener(new StepPagerStrip.OnPageSelectedListener() {
                @Override
                public void onPageStripSelected(int position) {
                    position = Math.min(mPagerAdapter.getCount() - 1,
                            position);
                    if (mPager.getCurrentItem() != position) {
                        mPager.setCurrentItem(position);
                    }
                }
            });

    mNextButton = (Button) findViewById(R.id.next_button);
    mPrevButton = (Button) findViewById(R.id.prev_button);

    mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            mStepPagerStrip.setCurrentPage(position);

            if (mConsumePageSelectedEvent) {
                mConsumePageSelectedEvent = false;
                return;
            }

            mEditingAfterReview = false;
            updateBottomBar();
        }
    });

    mNextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mPager.getCurrentItem() == mCurrentPageSequence.size()) {
                DialogFragment dg = new DialogFragment() {
                    @Override
                    public Dialog onCreateDialog(Bundle savedInstanceState) {
                        return new AlertDialog.Builder(getActivity())
                                .setMessage(R.string.submit_confirm_message)
                                .setPositiveButton(
                                        R.string.submit_confirm_button,
                                        null)
                                .setNegativeButton(android.R.string.cancel,
                                        null).create();
                    }
                };
                dg.show(getSupportFragmentManager(), "place_order_dialog");
            } else {
                if (mEditingAfterReview) {
                    mPager.setCurrentItem(mPagerAdapter.getCount() - 1);
                } else {
                    mPager.setCurrentItem(mPager.getCurrentItem() + 1);
                }
            }
        }
    });

    mPrevButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        }
    });

    onPageTreeChanged();
    updateBottomBar();
}

@Override
public void onPageTreeChanged() {
    mCurrentPageSequence = mWizardModel.getCurrentPageSequence();
    recalculateCutOffPage();
    mStepPagerStrip.setPageCount(mCurrentPageSequence.size() + 1); // + 1 =
                                                                    // review
                                                                    // step
    mPagerAdapter.notifyDataSetChanged();
    updateBottomBar();
}

private void updateBottomBar() {
    int position = mPager.getCurrentItem();
    if (position == mCurrentPageSequence.size()) {
        mNextButton.setText(R.string.finish);
        mNextButton.setBackgroundResource(R.drawable.finish_background);
        mNextButton.setTextAppearance(this, R.style.TextAppearanceFinish);
    } else {
        mNextButton.setText(mEditingAfterReview ? R.string.review
                : R.string.next);
        mNextButton
                .setBackgroundResource(R.drawable.selectable_item_background);
        TypedValue v = new TypedValue();
        getTheme().resolveAttribute(android.R.attr.textAppearanceMedium, v,
                true);
        mNextButton.setTextAppearance(this, v.resourceId);
        mNextButton.setEnabled(position != mPagerAdapter.getCutOffPage());
    }

    mPrevButton
            .setVisibility(position <= 0 ? View.INVISIBLE : View.VISIBLE);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mWizardModel.unregisterListener(this);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBundle("model", mWizardModel.save());
}

@Override
public AbstractWizardModel onGetModel() {
    return mWizardModel;
}

......
}
}

2 个答案:

答案 0 :(得分:10)

使用getActivity()方法。这将返回片段的封闭活动。

Intent firstpage= new Intent(getActivity(),FragmentWizard.class);
getActivity().startActivity(firstpage); // You same startActivity method in fragment as well you can use any of this

public final Activity getActivity ()

答案 1 :(得分:2)

您必须使用onActivityCreated方法来使用它:

在你的课堂上,只需:

public class FragmentMain extends Fragment {


public FragmentMain() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_main,container, false);

    return view;
}
}

然后立即使用onActivityCreated方法: 这是代码:

 @Override
        public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
    TextView textView = (TextView) getActivity().findViewById(R.id.fragment_main_textview);
   Button x = (Button) getActivity().findViewById(R.id.deneme);
    x.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent firstpage= new Intent(getActivity(),FragmentWizard.class);
            getActivity().startActivity(firstpage);
        }
    });



    }

希望这可行..:)