Qube
Qube

Reputation: 563

How to intent from my activity to fragment in my tab bar?

i'm trying to intent from my activity to activity with fragment. the intent is ok but what i want it is when it intent it will automatically call the fragment2 based on what it clicked in previous activity. i'm tring to make intent with getExtra to make if in activity of fragment to make if the id is 1 then call the fragment with the code below:

  String sessionId2;
    String id = null;
    String sessionId = getIntent().getStringExtra("baik");
    sessionId2 = getIntent().getStringExtra("diperbaiki");
    String sessionId3 = getIntent().getStringExtra("rusak");

    Log.d("baik",""+sessionId);
    Log.d("diperbaiki",""+sessionId2);
    Log.d("rusak",""+sessionId3);

     if (id == sessionId3) {
        TabLayout.Tab tab = tabLayout.getTabAt(1);
        tab.select();
    }else if (id == sessionId2) {
        TabLayout.Tab tab = tabLayout.getTabAt(2);
        tab.select();
    }

but it still not working. and i also try to change the position like the code below:

public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        if (id == sessionId2){
            position = 1;
        }

        switch (position) {
            case 0:
                // Top Rated fragment activity
                return new BaikFragment();
            case 1:
                // Games fragment activity
                return new DiperbaikiFragment();
            case 2:
                // Movies fragment activity
                return new RusakFragment();
        }

        return null;
    }

but it wont work too. please help is there is any idea?

Upvotes: 0

Views: 1012

Answers (2)

Jins Lukose
Jins Lukose

Reputation: 707

Easy way for using activity and tab is this one TabLayout using activities instead of fragments

Your_layout_id.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeTop() {
    Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();
}
public void onSwipeRight() {
    Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(this, SecondActivity.class);
    startActivity(intent);
}
public void onSwipeLeft() {
    Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(this, FirstActivity.class);
    startActivity(intent);
}
public void onSwipeBottom() {
    Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();
}

public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

});

Upvotes: 0

lasagnakid77
lasagnakid77

Reputation: 328

In your activitys onCreate or wherever it is that you setup the TabLayout you can then retrieve a specific Tab by using the number it is assigned (first tab you added to your view pager (adapter) should be at 0, second at 1):

TabLayout.Tab tab = tabLayout.getTabAt(0);

So i'd recommend you do something like this, after setting up your Tab Layout:

String sessionId= getIntent().getStringExtra("baik");
  if (id == sessionId){
            TabLayout.Tab tab = tabLayout.getTabAt(0);
            tab.select();
        }
  else if (....) {
            TabLayout.Tab tab2 = tabLayout.getTabAt(1);
            tab2.select();
  }

Let me know if this works!

Upvotes: 1

Related Questions