0

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;
}


}
Osmani
  • 35
  • 8
  • If you do not Have Anny Data that Dependent Each Other Activity Then call that Activity Again.. Else You can Also Pass Data In To Intent and OnActivity Of result You Can Call Agian With You Required data. – Learning Always Nov 24 '17 at 05:32
  • What I'm doing now is call the Activity 1 from the onKeyDown method in Activity 2, and when it shows the tab 1 is the one shown, i want to show the tab 2. – Osmani Nov 24 '17 at 05:41
  • It Is All Dpends On How You call Activity Initially To get data. Please Post You Some Code Here , that How You Handle You Activity First.. – Learning Always Nov 24 '17 at 05:43

2 Answers2

1

You can use several methods for this purpose. When to show which tab when Activity is resuming :

1.startactivityforresult :- This is very basic you can validate the result in "onActivityResult" What to do. But be aware it will not work if your Activity's launch mode is singleInstance.

2.localbroadcastmanager :- You can fire a local broadcast for the action .

3.eventbus :- This is the best way to communicate b/w Activities for large project(Where needed lots of communication )

ADM
  • 20,406
  • 11
  • 52
  • 83
0

Try this :

tabHost.setCurrentTab(number of tab you want to go);

Also check this :

Android Tab Help. How to set 2nd tab as default when app opens?

jakir hussain
  • 316
  • 2
  • 18