简体   繁体   English

在主机Activity中将两个片段添加到后台

[英]Adding two fragments to the backstack within host Activity

I currently have an Android Activity called MainActivity that hosts 2 Fragments (Fragment A and Fragment B) via the code below. 我目前有一个名为MainActivity的Android Activity ,该Activity通过以下代码托管2个Fragments (片段A和片段B)。 I'm using a TabLayout to navigate back and forth between Fragment A and Fragment B within MainActivity . 我正在使用TabLayoutMainActivity Fragment A和Fragment B之间来回导航。 Each Fragment includes a listView in addition to an onItemClick method. 每个Fragment除了onItemClick方法外还包括一个listView Once the item in the listView is clicked within both Fragment A and Fragment B the user is taken to a new Activity (Activity_1 and Activity_2 respectively. I need to override the back button in Activity 2, so that I'm taken back to Fragment B within the MainActivity . I'm having difficulty understanding where and how to add both Fragment A and Fragment B to the backstack , thus allowing me to hit the back button in Activity_1 and Activity_2 that takes me back to the Fragment that I navigated from. Where and how do I implement the backstack code for Fragment A and Fragment B in the MainActivity . Fragment A和Fragment B中单击listView的项目后,用户将被带到一个新的Activity (分别为活动1和活动2。我需要覆盖Activity 2中的后退button ,以便将我带回到Fragment B在MainActivity ,我很难理解在哪里以及如何将Fragment A和Fragment B都添加到backstack ,因此让我在Activity_1和Activity_2中单击“后退” button可以将我带回到从中导航的Fragment 。以及如何在MainActivity实现片段A和片段B的backstack代码。

片段

MainActivity.java MainActivity.java

public class MainActivity extends AppCompatActivity {

TabLayout tabLayout;
ViewPager viewPager;

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

    viewPager = (ViewPager) findViewById(R.id.viewPager);
    viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager(), getApplicationContext()));

    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    tabLayout.setupWithViewPager(viewPager);

}
private class CustomAdapter extends FragmentStatePagerAdapter{

    private String fragments [] = {"Fragments 1", "Fragments 2"};

    public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext){
        super(supportFragmentManager);
    }

    @Override
    public Fragment getItem(int position) {
        switch(position){
            case 0:
                return  new Fragment1();
            case 1:
                return  new Fragment2();
            default:
                return null;
        }
    }
    @Override
    public int getCount() {
        return fragments.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return fragments[position];
    }
  }
}

FragmentA.java FragmentA.java

public class FragmentA extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragA, container, false);
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    . . .

   }
}

FragmentB.java FragmentB.java

FragmentA.java

public class FragmentB extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragB, container, false);
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    . . .

   }
}

您可以使用startActivityForResult()启动新活动,并根据正在进行的活动返回一个常量,并在MainActivity中处理响应并显示所需的片段。

As long as your MainActivity knows (or can know) which fragment (A or B) handled click: 只要您的MainActivity知道(或可以知道)哪个片段(A或B)处理过,请单击:

  1. MainActivity should store state (information re which fragment handled click) MainActivity应该存储状态(有关片段处理点击的信息)
  2. Once restored, MainActivity should recreate UI based on saved state 还原后,MainActivity应该根据保存的状态重新创建UI
  3. Neither Activity1 nor Activity2 should know anything about invocator - they could finish by "up" or sys back, does not matter Activity1和Activity2都不应该知道有关Invocator的任何信息-它们可以通过“启动”或sys返回来完成,没关系

In case you are using FragmentStatePagerAdapter (or similar) to serve your tabs, it's even more simple. 如果您使用FragmentStatePagerAdapter(或类似的产品)来服务您的标签,则更加简单。

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

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