I have in my android application two Activities:
Activity A: It has a tab navigation view with 3 tabs. When user slide the screen from tab 1 to tab 2, the tab 2 view shows a button that goes to Activity.
2. What I want to do is when I press the Back button in Activity 2, the application shows Activity 1 on tab 2 and not tab 1 as is happening now. Hope any help please. Thanks in advance.
Below a piece of my code. Note that I avoid some code.
public class Activity1 extends ActionBarActivity
implements ActionBar.TabListener,ViewPager.OnPageChangeListener,NavigationDrawerFragment.NavigationDrawerCallbacks {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tab = actionBar.newTab().setText(R.string.tab_one).setTabListener(this);
actionBar.addTab(tab);
tab = actionBar.newTab().setText(R.string.tab_two).setTabListener(this);
actionBar.addTab(tab);
tab = actionBar.newTab().setText(R.string.tab_three).setTabListener(this);
actionBar.addTab(tab);
}
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
tab.getPosition();
return new Fragment_Tab_1();
case 1:
tab.getPosition();
return new Fragment_Tab_2();
case 2:
tab.getPosition();
return new Fragment_Tab_3();
default:
return null;
}
}
public int getCount() {
return 3;
}
}
}
public class Fragment_Tab_2 extends Fragment { Button show_activity2;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
bundle = savedInstanceState;
container2 = container;
if (container == null) {
return null;
}
LinearLayout relative_layout = (LinearLayout) inflater.inflate(R.layout.fragment_Tab_2, container, false);
context = relative_layout.getContext();
show_activity2 = (ImageButton) relative_layout.findViewById(R.id.show_activity2);
show_activity2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Activity2.class);
startActivity(intent);
}
});
return relative_layout;
}
}
public class Activity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)){
Intent intent = new Intent(Activity2.this,Activity1.class);
startActivity(intent);
finish();
}
return true;
}
}