0

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?

Qube
  • 543
  • 3
  • 9
  • 23
  • what is the error, did you try to debug your code? – karan Oct 17 '18 at 09:56
  • Do you use a tab layout? – lasagnakid77 Oct 17 '18 at 10:03
  • yes i use tab layout @lasagnakid77 – Qube Oct 17 '18 at 10:08
  • there is no error. it just don't want to change the fragment when i intent it. what i want it when i intent it to the activity of the fragment it will change the fragment @KaranMer – Qube Oct 17 '18 at 10:10
  • Just so understand this correctly, you want to switch to a specific tab from the activity with tabs in it or a fragment that is not a tab? – lasagnakid77 Oct 17 '18 at 10:12
  • from my activity i will intent it to other activiy with tabs in it. there are 3 tabs on it. and i want it for example pass it to 2nd tab or the other tab based on what i clicked in previous activity before intent @lasagnakid77 – Qube Oct 17 '18 at 10:19

2 Answers2

1

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!

lasagnakid77
  • 318
  • 2
  • 14
  • I'd suggest you make a Toast to show you that the intent is working as intended. Also add the tab activity code into your main post. – lasagnakid77 Oct 17 '18 at 11:43
  • it actually works but it kinda weird. it wont follow the id that i send. the code i just add it above. it will change to rusak fragment if `id==session2` and both baik and diperbaiki fragment will change if `id==session3` and i have no idea how to change baik fragment in the right place – Qube Oct 17 '18 at 12:49
  • and the intent actually works well. but maybe my if statement was wrong – Qube Oct 17 '18 at 12:56
  • Does my code/solution work or something else? If it my solution please accept it as the solution, otherwise edit your post with your solution so other people will know if they have a similar problem. Glad to help (if I did :) ) – lasagnakid77 Oct 17 '18 at 14:52
  • your code works. the promblem is just the intent i made – Qube Oct 18 '18 at 07:24
0

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

});

Jins Lukose
  • 699
  • 6
  • 19