Call FragmentActivity from Fragment

user3086033

Can I call a fragmentactivity from a fragment?

If I can call,how can it be done?

Otherwise, how can I solve this problem ?

In my code, I want to call FragmentWizard.class from the "FragmentMain" fragment,but this doesn't compile.

Below is my code:

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;
}

......
}
}
Triode

use getActivity() method. Which will return the enclosing activity for the fragment.

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 ()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Call an activity method from a fragment

From Dev

Get id of TextView in Fragment from FragmentActivity in ViewPager

From Dev

Fragment transaction with a FragmentActivity instead of a Fragment

From Dev

Call method in fragment from another fragment/activity

From Dev

fragment run before fragmentActivity

From Dev

Android - Intent Acitivity to a Fragment of a FragmentActivity

From Dev

refresh fragment UI from fragmentActivity

From Dev

call datepicker from a fragment

From Dev

How to call fragment from activity without using fragmentactivity?

From Dev

Import text from fragmentactivity to fragment

From Dev

Android - How to call a fragment from inside a fragment?

From Dev

Android - How to access a Fragment from a FragmentActivity

From Dev

How to call Fragment from Fragment along with data

From Dev

(Android) how to call method in fragment from fragmentActivity

From Dev

How to call a fragment layout from the FragmentActivity to avoid NullPointerException

From Dev

How to call the AsyncTask in the fragment from other fragment?

From Dev

Android - Back button from FragmentActivity to another Fragment

From Dev

FragmentActivity not showing the right layout for the fragment

From Dev

Call method in fragment from another fragment/activity

From Dev

Android problems with fragment and fragmentActivity

From Dev

Fragment being launched from FragmentActivity not showing up

From Dev

refresh fragment UI from fragmentActivity

From Dev

Sending a string from FragmentActivity to Fragment through FragmentPagerAdaptor

From Dev

How to get a Fragment instance from FragmentActivity which contains a ViewPager for fragment transactions?

From Dev

How to pass parameter to Fragment from FragmentActivity

From Dev

call method from fragment to fragment ( refresh adapter )

From Dev

Open new Fragment from FragmentActivity

From Dev

Refresh Fragment in FragmentActivity

From Dev

Android Call Fragment Method from Fragment

Related Related

  1. 1

    Call an activity method from a fragment

  2. 2

    Get id of TextView in Fragment from FragmentActivity in ViewPager

  3. 3

    Fragment transaction with a FragmentActivity instead of a Fragment

  4. 4

    Call method in fragment from another fragment/activity

  5. 5

    fragment run before fragmentActivity

  6. 6

    Android - Intent Acitivity to a Fragment of a FragmentActivity

  7. 7

    refresh fragment UI from fragmentActivity

  8. 8

    call datepicker from a fragment

  9. 9

    How to call fragment from activity without using fragmentactivity?

  10. 10

    Import text from fragmentactivity to fragment

  11. 11

    Android - How to call a fragment from inside a fragment?

  12. 12

    Android - How to access a Fragment from a FragmentActivity

  13. 13

    How to call Fragment from Fragment along with data

  14. 14

    (Android) how to call method in fragment from fragmentActivity

  15. 15

    How to call a fragment layout from the FragmentActivity to avoid NullPointerException

  16. 16

    How to call the AsyncTask in the fragment from other fragment?

  17. 17

    Android - Back button from FragmentActivity to another Fragment

  18. 18

    FragmentActivity not showing the right layout for the fragment

  19. 19

    Call method in fragment from another fragment/activity

  20. 20

    Android problems with fragment and fragmentActivity

  21. 21

    Fragment being launched from FragmentActivity not showing up

  22. 22

    refresh fragment UI from fragmentActivity

  23. 23

    Sending a string from FragmentActivity to Fragment through FragmentPagerAdaptor

  24. 24

    How to get a Fragment instance from FragmentActivity which contains a ViewPager for fragment transactions?

  25. 25

    How to pass parameter to Fragment from FragmentActivity

  26. 26

    call method from fragment to fragment ( refresh adapter )

  27. 27

    Open new Fragment from FragmentActivity

  28. 28

    Refresh Fragment in FragmentActivity

  29. 29

    Android Call Fragment Method from Fragment

HotTag

Archive