【问题标题】:How can I go back to main activity from a Fragment如何从片段返回主要活动
【发布时间】:2021-03-04 15:33:09
【问题描述】:

我成功实现了底部导航抽屉并使用了片段。现在我可以从抽屉菜单中切换片段。 我的问题是,当我从抽屉菜单中打开一个项目(例如;关于开发人员)时,当用户单击后退按钮时,它会关闭应用程序。相反,我希望用户从任何片段返回到主活动。帮帮我吧,我没有太多经验。

这是我的代码;

我如何使用抽屉适配器和自定义库(SlidingRootNav-https://github.com/yarolegovich/SlidingRootNav)实现导航抽屉项目;

@Override
    public void onItemSelected(int position) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if (position == POS_DASHBOARD){

            slidingRootNav.closeMenu();
            transaction.addToBackStack(null);
            transaction.commit();

        }

        else if (position == POS_ABOUT_APP){
            ((CoordinatorLayout)findViewById(R.id.root_view)).removeAllViews();

            AboutAppFragment aboutApp = new AboutAppFragment();
            transaction.replace(R.id.container, aboutApp);

            slidingRootNav.closeMenu();
            transaction.addToBackStack(null);
            transaction.commit();

        }
        else if (position == POS_ABOUT_DEVELOPERS){
            ((CoordinatorLayout)findViewById(R.id.root_view)).removeAllViews();

            AboutDeveloper aboutDeveloper = new AboutDeveloper();
            transaction.replace(R.id.container, aboutDeveloper);

            slidingRootNav.closeMenu();
            transaction.addToBackStack(null);
            transaction.commit();

        }
        

    }

片段类

import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



public class AboutDeveloper extends Fragment  {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_about_developer,container, false);


        // Here I implemented a toolbar at the top for navigation purpose
        // but on clicking on the nav icon, the app closes, instead i want to return to the main activity

        Toolbar toolbar = root.findViewById(R.id.return_home);
        toolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_back_24);
        toolbar.setTitle("About Developer");


        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().onBackPressed();
            }
        });
        return root;

    }

}

【问题讨论】:

    标签: java android android-fragments navigation-drawer


    【解决方案1】:

    使用add 代替replace 进行交易

    【讨论】:

      【解决方案2】:

      我认为这是因为您在方法 addToBackStack 中有 null

       transaction.addToBackStack(null)
      

      因此,当您单击后退按钮时,您不会获得主要活动。

      如果您想了解有关 addToBackStack 工作原理的更多信息,我建议您阅读此What is the meaning of addToBackStack with null parameter?

      【讨论】:

      • 在我的 addToBackStack 方法中传递 null 参数不是问题。事实上它是解决方案的一部分,因为我需要它来保持活动片段的计数。感谢您的回复:)
      【解决方案3】:

      在做了一些挖掘之后,我终于找到了解决方法。我所要做的就是从主要活动(在我的例子中为 HomeActivity.class)对我的 onBackPressed 方法进行调整。

      @Override
          public void onBackPressed() {
      
              //Return to home activity from any active fragment
              if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
                  Intent intent = new Intent(this, HomeActivity.class);
                  startActivity(intent);
              }
      
              //To close nav drawer
              else if(slidingRootNav.isMenuOpened()){
                  slidingRootNav.closeMenu();
      
              }
              else {
                  moveTaskToBack(true);
              }
      
          }
      

      NB addToBackStack 必须在片段事务期间使用,以记录片段何时处于活动状态。 说transaction.addToBackStack(null)

      至于我的 Fragment 类中的顶部导航,我所做的只是添加一个 Intent,当单击工具栏导航图标时调用 home 活动。

      public class AboutDeveloper extends Fragment  {
      
          Intent intent;
      
      
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_about_developer,container, false);
      
      
              intent = new Intent(getActivity(), HomeActivity.class);
      
              Toolbar toolbar = root.findViewById(R.id.return_home);
              toolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_back_ios_24);
              toolbar.setTitle("About Developer");
              toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      startActivity(intent);
                      getActivity().finish();
                  }
              });
              return root;
      
          }
      
      
      }
      

      感谢提出建议的人。

      【讨论】:

        猜你喜欢
        • 2015-08-30
        • 1970-01-01
        • 1970-01-01
        • 2016-09-20
        • 2021-10-13
        • 2022-01-25
        • 1970-01-01
        • 1970-01-01
        • 2013-02-17
        相关资源
        最近更新 更多