我有3个片段的mainActivity。在导航时,从其中一个片段到另一个活动,并按下应用程序上的后退图标将我带到mainActivity的第一个片段。我如何回到我导航的那个而不是第一个?这是MainActivity的代码。
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
int counter = 0;
private String[] tabs = { "Home", "Top Locations", "Events"};
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//For hiding the title and preventing lag
setTheme(R.style.Theme_Example);
setContentView(R.layout.activity_main);
// Initialization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
//To not display the title on actionbar. can be done by android:label="" as well
getActionBar().setDisplayShowTitleEnabled(false);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Changing color of action bar
// actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#f0c914")));
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
// This code puts action bar and tabs in 2 lines
Method setHasEmbeddedTabsMethod=null;
try
{
setHasEmbeddedTabsMethod = actionBar.getClass().getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
}
catch(NoSuchMethodException e1)
{
e1.printStackTrace();
}
setHasEmbeddedTabsMethod.setAccessible(true);
try
{
setHasEmbeddedTabsMethod.invoke(actionBar, false);
} catch(IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//On Tab selection the current tab should be displayed.
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
以下是其中一个片段的代码
public class TopLocations extends Fragment implements OnClickListener {
ImageButton b1,b2,b3,b4,b5,b6,b7,b8;
public static int flag;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.top_locations, container, false);
//We need object of View in Fragments since findViewById doesn't work with Fragments
b1 = (ImageButton)rootView.findViewById(R.id.btnn);
b1.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//We need getActivity() method for intents within Fragments
switch(arg0.getId())
{
case R.id.btnn:
Intent a = new Intent (getActivity(),mjclass.class);
startActivity(a);
break;
}
}
}
当我使用app back图标从mjclass返回时,它会将我带到MainActivity的主片段,而不是从片段开始的“Top Location”片段