问题
Here again!
The situation is this, I have:
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];
}
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);
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.
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.
回答1:
Finally I've found the solution by my own :)
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!
来源:https://stackoverflow.com/questions/14375114/open-a-specific-fragment-of-activity-a-from-activity-b