0

I have a Activity named as Wallet and have a Activity named as CRechargeMain which adds two frgament named as "Mobile","Data";what I want in Wallet screen I have a Listview in which in case 0: when I click I want to go to CRechargeMain and show"Mobile" tab and in case 1 : when I click I want to go CRechareMain and open tab " data" .how can I do that code for wallet:-

m_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
                case 0:
                    Intent mMobileRecharges = new Intent(CMyWalletScreen.this,CRechargeMain.class);
                    startActivity(mMobileRecharges);

                    break;
                case 1:
                    Intent mDataRecharge = new Intent(CMyWalletScreen.this,CRechargeMain.class);
                    startActivity(mDataRecharge);
                    break;
                case 2:
                    Intent m_Earning= new Intent(CMyWalletScreen.this,CWalletTransactionScreen.class);
                    startActivity(m_Earning);
                    break;

            }
        }
    });

code for CRechargeMain:-

public class CRechargeMain extends AppCompatActivity {
View m_Main;
private ViewPager m_ViewPager;
private Toolbar m_ToolBar;
private String[]actonBar={"Mobile Recharge","Mobile Data Recharge"};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recharge_main);
    init();
}

public void init() {
    m_ToolBar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(m_ToolBar);
    //noinspection ConstantConditions
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    m_ToolBar.setTitle("Mobile Recharge");

    TabLayout m_TabLayout = (TabLayout) findViewById(R.id.tab_layout);// finding Id of tablayout
    m_TabLayout.addTab(m_TabLayout.newTab().setText("Mobile"));// add deal listin tab
    m_TabLayout.addTab(m_TabLayout.newTab().setText("Data Card"));// add stories tab
    m_TabLayout.setTabGravity(TabLayout.GRAVITY_FILL);// setting Gravity of Tab

    m_ViewPager = (ViewPager) findViewById(R.id.pager);//finding Id of ViewPager
    CRechargePager m_oMobilePager = new CRechargePager
            (getSupportFragmentManager(), m_TabLayout.getTabCount());
    m_ViewPager.setAdapter(m_oMobilePager);// adiing adapter to ViewPager
    m_ViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(m_TabLayout));// performing action of page changing
    m_TabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            m_ViewPager.setCurrentItem(tab.getPosition());
            m_ToolBar.setTitle(actonBar[tab.getPosition()]);
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_wallet, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

}

Siddharth
  • 181
  • 2
  • 11

3 Answers3

0

You can use the same intent and select tab based on the intent argument in your activity.

Shrinidhi K
  • 28
  • 1
  • 8
0
Intent mMobileRecharges = new Intent(CMyWalletScreen.this,CRechargeMain.class);
intent.putExtra("doWhat", 0);

Intent mDataRecharge = new Intent(CMyWalletScreen.this,CRechargeMain.class);
intent.putExtra("doWhat", 1);

In your receiving activity:

int iDoWhat = intent.getIntExtra("doWhat", -1); 

Then make your decision based on the value of iDoWhat.

int iDoWhat = getIntent().getIntExtra("doWhat",-1);
switch (iDoWhat) {
case -1:
    //select tab 0
    break;
case 0:
    //select tab 0
    break;
case 1:
    //select tab 1
    break;
}

You have to write the code for selecting the tab now you know which one.

usajnf
  • 522
  • 3
  • 11
0

As my assumption you are having two activity, activity1 contains list and activity2 contains fragments.from activity1 based on conditions you have to jump to activity2 (fragments).Just call as below

Intent i =New Intent(this,CRechargeMain.class);
i.putExtra("",0);
startActivity(i);

In Activty2 on create method based on conditions you have to set fragment to load.

 FragmentManager FM = getFragmentManager();
FM.beginTransaction().replace(R.id.content_frame, detail).commit();