简体   繁体   中英

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. I'm using a TabLayout to navigate back and forth between Fragment A and Fragment B within MainActivity . Each Fragment includes a listView in addition to an onItemClick method. 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 .

片段

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

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

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:

  1. MainActivity should store state (information re which fragment handled click)
  2. Once restored, MainActivity should recreate UI based on saved state
  3. Neither Activity1 nor Activity2 should know anything about invocator - they could finish by "up" or sys back, does not matter

In case you are using FragmentStatePagerAdapter (or similar) to serve your tabs, it's even more simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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