gpt4 book ai didi

java - 从 Activity B 打开 Activity A 的特定片段

转载 作者:搜寻专家 更新时间:2023-11-01 01:06:31 25 4
gpt4 key购买 nike

又来了!

情况是这样的,我有:

Activity A 实现了一个 viewPager 并可视化了 3 个可能的片段。要访问每个片段,我使用此代码:

    @Override
public Fragment getItem(int page) {
switch (page) {
case 0: return new MyFirstFragment();
case 1: return new MySecondFragment();
case 2: return new MyThirdFragment();
}
return null;
}

@Override
public int getCount() {
return [the count of total fragments];
}

片段 3 包含一个用户列表。当我点击用户时,Activity B 启动。使用意图:

// Create new Intent Object, and specify class
Intent intent = new Intent();
intent.setClass(Fragment3.this, ActivityB.class);
//Use startActivity to start Activity B
startActivity(intent);

在 Activity B 中,有一个按钮将我重定向到 Fragment 2。所以问题是:我怎样才能返回看到 Fragment 2?我正在考虑重新开始 Activity A 并使用 putExtra 指定应可视化哪个片段。

例如,我将传递一个数字,在本例中为 2,并希望调用函数 Fragment getItem(2) 来可视化该片段。但是,Fragment getItem 包含在 pageadapter 类中,因此我不清楚如何进行。

最佳答案

最后我自己找到了解决方案:)

我使用 intent.putExtra() 来传递我想要显示的片段的位置,然后在调用的 Activity 中我使用了 ViewPager 的方法 setCurrentItem(position) 并显示了想要的片段。

希望这可以帮助遇到我同样问题的其他人!

关于java - 从 Activity B 打开 Activity A 的特定片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14375114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com