从活动B打开活动A的特定片段 [英] Open a specific fragment of activity A from activity B

查看:71
本文介绍了从活动B打开活动A的特定片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再来一次!

这种情况,我有:

Activity A,它实现一个viewPager并可视化3个可能的片段.要访问每个片段,我使用以下代码:

Activity A that implements a viewPager and visualizes 3 possible fragments. To access to each fragment I use this code:

    @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包含用户列表.当我单击一个用户时,活动B开始.使用意图:

Fragment 3 contains a list of users. When I click on a user Activity B is started. Using intent:

// 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);

在活动B中,有一个将我重定向到片段2的按钮.所以问题是:如何返回见片段2?我正在考虑再次开始活动A,并使用putExtra指定应该可视化的片段.

In activity B there is button that redirects me to Fragment 2. So the question is: how can I return see Fragment number 2? I was thinking to start again the activity A and using putExtra to specify which fragment should be visualized.

例如,在这种情况下,我将传递一个数字2,并希望调用Fragment getItem(2)函数以可视化该片段.但是,Fragment getItem包含在pageadapter类中,因此我不清楚如何继续.

For example I would pass a number, 2 in this case, and would like to call the function Fragment getItem(2) to visualize the fragment. However, Fragment getItem is contained in the pageadapter class, so I am unclear about how to proceed.

推荐答案

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

Finally I've found the solution by my own :)

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

I used intent.putExtra() to pass the position of the fragment which I wanted to display, then in the called activity I used the method setCurrentItem(position) of ViewPager and the wanted fragment is displayed.

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

Hope this can help someone else who will have my same problem!

这篇关于从活动B打开活动A的特定片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆