按下按钮后是否可以启动片段新的片段活动?

时间:2015-11-16 19:01:28

标签: android android-fragments

只是一个小问题,也许很难,也许不是。 但正如标题所说,按下按钮后是否可以启动新的fragmentActivity?

现在我一直在研究这个问题,我宁愿不再开始新的活动,除非我不得不去做。 到目前为止,我对我的结果感到满意,因为我尝试在一个类中为我拥有的3个不同按钮制作三个不同的片段包。

因此简而言之,有3个按钮,他们需要激活自己的片段包。

这是我想要修复的当前代码:

MainActivity:

public class MainActivity extends Activity {
    //ViewPager viewpager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       /* viewpager = (ViewPager)findViewById(R.id.pager);
        PagerAdapter padater = new PagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(padater);*/
    }

    public boolean contentSwitch = false;

    @Override
    public void onBackPressed() {
        if(contentSwitch) {
            finish();
            Log.i("BtnHandleEvent", "BackButton Pressed!");
            Intent startIntent = new Intent(MainActivity.this, MainActivity.class);
            startActivity(startIntent);
            contentSwitch = false;
        } else {
            Log.i("Event Handler", "Terminating app!");
            Runtime.getRuntime().runFinalization(); //No idea why I did this
            Runtime.getRuntime().gc();              //No idea why I did this too
            Runtime.getRuntime().exit(1);           //Doesn't cleanup?
        }
    }

    //start fragment activity
    public void btn1(View v) {
        contentSwitch = true;
    }

    public void btn2(View v) {
        contentSwitch = true;
    }

    public void btn3(View v) {
        contentSwitch = true;

    }}

FragmentPackage:

public class FragmentPackage extends FragmentPagerAdapter {
    public static boolean ViewPackOne = false; //Still to do

    //Still need to fix
    public FragmentPackage(FragmentManager fm) {
        super(fm);
    }

    public Fragment getItem(int arg0) {
        switch(arg0) {
            case 0:
                if(ViewPackOne) {
                    return new FragmentOne();
                }
            case 1:
                if(ViewPackOne){
                    return new FragmentTwo();
                }
            case 2:
                if(ViewPackOne) {
                    return new FragmentThree();
                }

            default:
                break;
        }
        return null;
    }

    public int getCount() {

        return 3;
    }

    public static class FragmentOne extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
            return inflater.inflate(R.layout.fragment_one_layout, container, false);
        }
    }

    public static class FragmentTwo extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
            return inflater.inflate(R.layout.fragment_two_layout, container, false);
        }
    }

    public static class FragmentThree extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
            return inflater.inflate(R.layout.fragment_one_layout, container, false);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

FragmentActivity是一个活动的子类,所以它也有 startActivity()方法

假设我们想从片段

启动ActivityB
Intent intent = new Intent(getActivity(), ActivityB.class);
startActivity(intent);

FragmentActivity

开始
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);