使后退按钮从活动返回到Android中的片段

Posted

技术标签:

【中文标题】使后退按钮从活动返回到Android中的片段【英文标题】:Make back button returns from an activity to a fragment in Android 【发布时间】:2016-10-14 08:10:52 【问题描述】:

我正在开发一个 android 应用程序,我有一个名为“更多”的片段,在这个片段中我有诸如“关于我们”、“条款和条件”之类的东西,这些都是正常的活动。我想要做的是,当我点击里面的后退按钮时,例如,关于我们的活动,我想要返回到“更多”片段。我怎样才能做到这一点?这是我在 aboutUs 活动中的内容:

“onBackPressed 方法应该在我单击后退按钮后返回到片段,但它不起作用。应用程序崩溃并且在日志中猫说“你是否在清单中声明了 MoreFragment 活动? ",但我试图声明它不起作用。

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

【问题讨论】:

这是在两层之内吗?当您单击 about 片段内的后退按钮时,现在会发生什么? 在这种情况下不要覆盖onBackPress 方法。它应该会自动工作。 【参考方案1】:

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

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

像这样更改您的代码:

 @Override
    public void onBackPressed()
    
        finish();
    

【讨论】:

我改成了这个,但没有用。它不是回到“更多”片段,而是回到标签中的第一个片段。 您能否分享第一个包含更多片段的活动的代码,然后启动 AboutUs 活动? 刚刚添加了调用activity的More Fragment。 刚刚添加了活动。 您的问题是由于您的活动的 OnStart 中的代码而发生的。只需将该代码移至 onCreate 即可解决您的问题。

以上是关于使后退按钮从活动返回到Android中的片段的主要内容,如果未能解决你的问题,请参考以下文章

应用ActionBar中的返回按钮

带有片段的 Android Up 按钮未显示完整片段

工具栏 - 只有一个活动从抽屉切换到后退按钮

如何摆脱Android应用栏中的后退按钮

如何防止在 Android 片段/活动中意外退出应用程序?

从android中的片段管理工具栏的导航和后退按钮