繁体   English   中英

在 Android Studio 中处理两个不同活动中的两个片段

[英]Handling two fragments in two different activities in Android Studio

我有两个活动,每个活动都加载一个片段。 在第一个活动中,我有一个具有列表视图的片段,当我单击列表视图项时,它会将我带到另一个使用 Intent 的活动。 片段在第二个活动中加载。 直到这里它工作正常。

现在,当我单击设备的后退按钮时,再次 go 返回列表视图,我必须单击 2 到 3 次 go 到上一个列表视图活动,第二个活动或片段在我返回第一个活动之前重新加载几次。

无法理解问题。

提前致谢!!!

在第一个活动中,这就是我加载片段的方式

fragment = new GrowSapceTutorialFragment();
                   fragmentManager = getSupportFragmentManager();
                    fragmentTransaction=fragmentManager.beginTransaction();




fragmentTransaction.replace(R.id.frame_container_grow_tutorial, fragment);
                    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    fragmentTransaction.commit();

对于第一个活动中的后退按钮:

@Override
    public void onBackPressed() {
        super.onBackPressed();

在 growspaceTutorialfragment 下的 oncreate viewenter 代码这里:

  listViewTip.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                for(int i=0; i<=position;i++){

                    name = dataModelArrayList.get(position).getTitle().toString();
                    description = dataModelArrayList.get(position).getDescription().toString();
                    image = dataModelArrayList.get(position).getImageUrl().toString();
                    tipIntent = new Intent(getActivity(), TipDetailsActivity.class);
                    tipIntent.putExtra("Title",name);
                    tipIntent.putExtra("Des", description);
                    tipIntent.putExtra("Image", image);
                startActivity(tipIntent);

在我的提示详细信息活动(第二个活动)中:

tipImage = findViewById(R.id.tipImageView);

        title = getIntent().getStringExtra("Title");
        description = getIntent().getStringExtra("Des");
        image = getIntent().getStringExtra("Image");
        Picasso.get().load(image).into(tipImage);

       bundle = new Bundle();
        bundle.putString("Title",title);
        bundle.putString("Des", description);


        fragment = new TipDetailsFragment();
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragment.setArguments(bundle);
        fragmentTransaction.detach(fragment);
        fragmentTransaction.attach(fragment);
        fragmentTransaction.replace(R.id.frame_container, fragment);
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        fragmentTransaction.commit();

提示详情 Activity 的后退按钮由以下方式处理:

  @Override
    public void onBackPressed() {
      super.onBackPressed();
}

提示详细信息片段:

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.fragment_tip_details, container, false);
        title = view.findViewById(R.id.textViewTipTitle);
        description = view.findViewById(R.id.textViewDescription);
        bundle = getArguments();
        tipTitle = bundle.getString("Title");
        tipDes = bundle.getString("Des");

title.setText(tipTitle);
        description.setText(tipDes);


        return view;

    }

活动和片段都有自己的生命周期。 当您加载第二个活动时。 第一个活动被终止,以便为第二个活动腾出空间。 当您按下后退按钮时,第二个被杀死,第一个被充气进入视野。 您需要将 go 返回您的代码并简化您的导航结构或发布您的导航代码以获得进一步的帮助。 干杯。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM