制作后退按钮从活动返回到Android

时间:2016-06-13 19:28:56

标签: android android-fragments button fragment back

我正在开发一个Android应用程序,我有一个名为" More"的片段,在这个片段中我有"关于我们","术语和条件",这是正常的活动。我想要做的是,当我点击里面的后退按钮时,例如,关于我们的活动,我想回到"更多"分段。我怎样才能做到这一点?这就是我在aboutUs活动中所拥有的:

一旦我点击后退按钮," onBackPressed方法应该返回到片段,但它不起作用。应用程序崩溃并在log cat中说“#34;你是否在清单中声明了MoreFragment Activity?"”但是我试图声明并且它不起作用。

public class AboutUs extends BaseActivity {

    private ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about_us);

        image = (ImageView) findViewById(R.id.imageView2);

        image.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                Intent launchactivity = new Intent(AboutUs.this, StartScreenActivity.class);

                startActivity(launchactivity);

            }

        });

    }

    @Override
    public void onBackPressed()
    {
        Intent intent = new Intent(this,MoreFragment.class);
        startActivity(intent);
    }
}

这是调用Activity的片段:

public class MoreFragment extends Fragment implements View.OnClickListener {


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_more, null, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        TextView lbl1 = (TextView) view.findViewById(R.id.lbl1);
        TextView lbl2 = (TextView) view.findViewById(R.id.lbl2);
        TextView lbl3 = (TextView) view.findViewById(R.id.lbl3);
        TextView lbl4 = (TextView) view.findViewById(R.id.lbl4);
        TextView lbl5 = (TextView) view.findViewById(R.id.lbl5);
        TextView lbl6 = (TextView) view.findViewById(R.id.lbl6);
        TextView lbl7 = (TextView) view.findViewById(R.id.lbl7);
        TextView lbl8 = (TextView) view.findViewById(R.id.lbl8);
        TextView lbl9 = (TextView) view.findViewById(R.id.lbl9);

        ImageView imv1 = (ImageView) view.findViewById(R.id.imv1);
        ImageView imv2 = (ImageView) view.findViewById(R.id.imv2);
        ImageView imv3 = (ImageView) view.findViewById(R.id.imv3);
        ImageView imv4 = (ImageView) view.findViewById(R.id.imv4);
        ImageView imv5 = (ImageView) view.findViewById(R.id.imv5);
        ImageView imv6 = (ImageView) view.findViewById(R.id.imv6);
        ImageView imv7 = (ImageView) view.findViewById(R.id.imv7);
        ImageView imv8 = (ImageView) view.findViewById(R.id.imv8);
        ImageView imv9 = (ImageView) view.findViewById(R.id.imv9);
        ImageView imv10 = (ImageView) view.findViewById(R.id.imv10);

        TextView btnSignOut = (TextView) view.findViewById(R.id.lbl10);

        lbl1.setOnClickListener(this);
        lbl2.setOnClickListener(this);
        lbl3.setOnClickListener(this);
        lbl4.setOnClickListener(this);
        lbl5.setOnClickListener(this);
        lbl6.setOnClickListener(this);
        lbl7.setOnClickListener(this);
        lbl8.setOnClickListener(this);
        lbl9.setOnClickListener(this);

        imv1.setOnClickListener(this);
        imv2.setOnClickListener(this);
        imv3.setOnClickListener(this);
        imv4.setOnClickListener(this);
        imv5.setOnClickListener(this);
        imv6.setOnClickListener(this);
        imv7.setOnClickListener(this);
        imv8.setOnClickListener(this);
        imv9.setOnClickListener(this);

        btnSignOut.setOnClickListener(this);

        View view9 = view.findViewById(R.id.view9);
        View view8 = view.findViewById(R.id.view8);

        if (!PrefernceHelper.getBoolean(getActivity(), Commons.Constants.USERLOGGEDIN)) {
            imv8.setVisibility(View.INVISIBLE);
            lbl8.setVisibility(View.INVISIBLE);
            imv10.setVisibility(View.INVISIBLE);
            btnSignOut.setVisibility(View.INVISIBLE);
            view9.setVisibility(View.INVISIBLE);
            view8.setVisibility(View.INVISIBLE);
        }

    }

    private void callAllMoreScreen(String type, Class<?> className) {

        Intent intent = new Intent();
        intent.setClass(getActivity(), className);
        intent.putExtra(Commons.Constants.LOAD_TYPE_URL, type);
        getActivity().startActivity(intent);

    }

    private void callAllMoreScreen1(Class<?> className) {

        Intent intent = new Intent();
        intent.setClass(getActivity(), className);
        getActivity().startActivity(intent);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.lbl1:
                callAllMoreScreen1(AboutUs.class);
                break;
            case R.id.lbl2:
                callAllMoreScreen("contact-us", AllMoreScreenActivity.class);
                break;
            case R.id.lbl3:
                callAllMoreScreen1(TermsConditions.class);
                break;
            case R.id.lbl4:
                callAllMoreScreen1(PrivacyPolicy.class);
                break;
            case R.id.lbl5:
                callAllMoreScreen("faqs", AllMoreScreenActivity.class);
                break;
            case R.id.lbl6:
                callAllMoreScreen("how-it-work", AllMoreScreenActivity.class);
                break;
            case R.id.lbl7:
                callAllMoreScreen("about-foodlebee", AllMoreScreenActivity.class);
                break;
            case R.id.lbl8:
                callAllMoreScreen(" ", PredefinedAddressActivity.class);
                break;
            case R.id.lbl9:
                callAllMoreScreen(" ", TrackyourorderActivity.class);
                break;
            case R.id.imv1:
                callAllMoreScreen1(AboutUs.class);
                break;
            case R.id.imv2:
                callAllMoreScreen("contact-us", AllMoreScreenActivity.class);
                break;
            case R.id.imv3:
                callAllMoreScreen1(TermsConditions.class);
                break;
            case R.id.imv4:
                callAllMoreScreen1(PrivacyPolicy.class);
                break;
            case R.id.imv5:
                callAllMoreScreen("faqs", AllMoreScreenActivity.class);
                break;
            case R.id.imv6:
                callAllMoreScreen("how-it-work", AllMoreScreenActivity.class);
                break;
            case R.id.imv7:
                callAllMoreScreen("about-foodlebee", AllMoreScreenActivity.class);
                break;
            case R.id.imv8:
                callAllMoreScreen(" ", PredefinedAddressActivity.class);
                break;
            case R.id.imv9:
                callAllMoreScreen(" ", TrackyourorderActivity.class);
                break;
            case R.id.lbl10:
                PrefernceHelper.clearAll(getActivity());
                callAllMoreScreen("", SignInActivity.class);
                getActivity().finish();
                break;
        }
    }
}

这是调用片段的Activity:

public class TabLayoutScreenActivity extends BaseActivity {

    private TabLayout tabLayout;
    private Bundle bundle;
    //Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_screen);

        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar); // actionBarSetUp(true);

        // getSupportActionBar().setIcon(R.drawable.small_bee);
        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
        // getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //  getSupportActionBar().setIcon(R.drawable.ic_launcher);
        viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);
        ImageView foodlebee = (ImageView) findViewById(R.id.imageView2);

        foodlebee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent launchactivity = new Intent(TabLayoutScreenActivity.this,StartScreenActivity.class);
                startActivity(launchactivity);
            }

        });

        // bundle = getIntent().getExtras();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_login, menu);
        if (PrefernceHelper.getBoolean(this, Commons.Constants.USERLOGGEDIN)) {
            MenuItem item = menu.findItem(R.id.user_login);
            item.setVisible(false);
            invalidateOptionsMenu();
        }
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.user_login) {
            intentCall(SignInActivity.class, null, 0);
            return true;
        } else if (item.getItemId() == R.id.home) {
            intentCall(StartScreenActivity.class, null, 0);
            finish();
            return true;
        }
        return false;

    }

    @Override
    protected void onStart() {
        super.onStart();
        bundle = getIntent().getExtras();
        int tabPostion = 0;
        if (bundle != null)
            tabPostion = bundle.getInt(Commons.Constants.TAB_POSITION, 0);

        TabLayout.Tab tab = tabLayout.getTabAt(tabPostion);
        tab.select();
    }

    public class SectionPagerAdapter extends FragmentPagerAdapter {

        public SectionPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new ChefFragment();
                case 1:
                    return new OrderFragment();
                case 2:
                    return new BasketFragment();
                case 3:
                    return new MoreFragment();
                default:
                    return new ChefFragment();
            }
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Chefs";
                case 1:
                    return "Orders";
                case 2:
                    return "Cart";
                case 3:
                    return "More";
                default:
                    return "Chef";
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

刚刚完成()onbackPressed中的当前活动,因此您将返回更多片段,因为前一个活动已经在后台堆栈上。您无需再次启动上一个活动。

此外,您的代码是错误的...您正在尝试启动一个不可能的片段。这就是抛出此异常的原因。

更改您的代码:

 @Override
    public void onBackPressed()
    {
        finish();
    }