如何实现Intent从另一个片段调用片段

时间:2017-10-11 07:34:51

标签: android android-fragments

我正在使用标签式活动。我在第一个选项卡中使用了一个按钮,按钮单击该按钮应该到第二个选项卡。

如何使用Intent从First Fragment调用Second Fragment?

这是我的两个片段的代码。

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

第二片段:

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

我现在使用的代码在单击第一个按钮时显示以下错误:

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)

任何人都为我提供了解决这个问题的方法。谢谢..

3 个答案:

答案 0 :(得分:0)

你无法调用Intent来调用fragment。 如果要添加片段,则必须使用此代码。

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

或者如果你想替换片段

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

答案 1 :(得分:0)

Intent i = new Intent(getActivity(), PaymentFragment.class);
startActivity(i);

使用Fragment无法工作并启动startActivity()。当您只想更改Activity时必须使用它。

如果要替换Fragment,请使用FragmentManagerFragmentTransaction

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

请阅读此Replace One Fragment with Another,这对您有所帮助。

答案 2 :(得分:0)

为了让您的用户界面更具互动性和美观,我将分享我已实施的内容。

只需从片段的java文件中访问您的viewPager,请在标签式活动中声明您的viewPager,如下所示。

public static ViewPager viewPager;

然后在onClick方法或按钮的onClickListener中,您可以使用此功能。

TabbedActivity.viewPager.arrowScroll(View.FOCUS_RIGHT);

这将平滑滚动您的viewpager到付款活动,是的,如果您想限制用户直接滚动,您可以禁用viewPager的滚动,请阅读here

但这仅适用于您在选项卡式活动的FragmentAdapter中实施以下片段的情况。

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

            }
            return null;
        }

如果用户想要通过

返回ShippingFragment,您也可以将焦点移至左侧
TabbedActivity.viewPager.arrowScroll(View.FOCUS_LEFT);

是的,它在我发布答案之前首先进行测试,愚蠢的我。