0

I am using tabbed activity. I have used a button in the first tab for which on button click it should to the second tab.

How can I use Intent to call Second Fragment from First Fragment?

Here is the code of my both fragments.

First Fragment:

package com.touchsmart.spectacular.ui.Fragments;

import....

public class ShippingFragment extends Fragment {

  private LinearLayout addnew;

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    //returning our layout file

    View view = inflater.inflate(R.layout.fragment_shipping, container, false);

    addnew = (LinearLayout) view.findViewById(R.id.addnew);

    addnew.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(getActivity(),PaymentFragment.class);
            startActivity(i);
        }
    });


    return view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    //you can set the title for your toolbar here for different fragments different titles
    getActivity().setTitle("Check Out");
}
}

Second Fragment:

package com.touchsmart.spectacular.ui.Fragments;

import...

public class PaymentFragment extends Fragment{

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //returning our layout file
    View view = inflater.inflate(R.layout.fragment_payment, container, false);

    return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    //you can set the title for your toolbar here for different fragments different titles
    getActivity().setTitle("Check Out");
    }
    }

The code which I am using now shows the following error on clicking the first button:

FATAL EXCEPTION: main Process: com.touchsmart.spectacular, PID: 18942
    android.content.ActivityNotFoundException: Unable to find explicit activity class {com.touchsmart.spectacular/com.touchsmart.spectacular.ui.Fragments.PaymentFragment}; have you declared this activity in your AndroidManifest.xml?
    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
    at android.app.Activity.startActivityForResult(Activity.java:4225)
    at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
    at android.support.v4.app.ActivityCompatJB.startActivityForResult(ActivityCompatJB.java:30)
    at android.support.v4.app.ActivityCompat.startActivityForResult(ActivityCompat.java:146)
    at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:932)
    at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:1047)
    at android.support.v4.app.Fragment.startActivity(Fragment.java:944)
    at android.support.v4.app.Fragment.startActivity(Fragment.java:933)
    at com.touchsmart.spectacular.ui.Fragments.ShippingFragment$1.onClick(ShippingFragment.java:36)
    at android.view.View.performClick(View.java:5637)
    at android.view.View$PerformClick.run(View.java:22429)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

anyone provide me a solution to get through this.Thanks..

Darshuu
  • 501
  • 5
  • 9

3 Answers3

0

You can not call Intent to calling fragment. You have to use this code if you want to add fragment.

getSupportFragmentManager().beginTransaction().add(R.id.frameLayout, fragment).commit();

Or if you want to replace fragment

getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, fragment).commit();
Akshay Raiyani
  • 1,243
  • 9
  • 21
0
Intent i = new Intent(getActivity(), PaymentFragment.class);
startActivity(i);

It can't work and start Fragment by using startActivity(). you have to use it when you want to change only Activity.


When you want to replace Fragment, use FragmentManager and FragmentTransaction.

// Create fragment and give it an argument specifying the article it should show
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

Please read this Replace One Fragment with Another, It will be helpful for you.

kimkevin
  • 2,202
  • 1
  • 26
  • 48
0

Just to make your UI more Interactive and good looking, I'm gonna share what I've implemented.

Just to access your viewPager from the fragment's java file, declare your viewPager as below in your tabbed activity.

public static ViewPager viewPager;

Then in the onClick method or onClickListener of a button, you can just use this.

TabbedActivity.viewPager.arrowScroll(View.FOCUS_RIGHT);

This will smooth scroll your viewpager to the Payment activity, and yes, if you want to limit the user from scrolling directly you can disable the scrolling of viewPager, read here.

But this only works if you've implemented the fragments as below in the FragmentAdapter in tabbed activity.

@Override
        public android.support.v4.app.Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new ShippingFragment ();
                case 1:
                    return new PaymentFragment ();

            }
            return null;
        }

You can also shift the focus to left if user wants to go back to ShippingFragment by just

TabbedActivity.viewPager.arrowScroll(View.FOCUS_LEFT);

And yes, it's working as I first test before posting answers, silly me.

Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50