问题
T have two Activities: MainActivity and LoginActivity. I have one Fragment too, the name is TabFragment (with a TabLayout), its parent is MainActivity. How can I intent into the specific TabFragment from my Login Activity.
This is my Main Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
id = getIntent().getStringExtra("id");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.confrag, new FragmentUtama()).commit();
getSupportFragmentManager().beginTransaction().replace(R.id.confrag, new TabFragment()).commit();
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
sharedPreferences = getSharedPreferences("login.conf", MODE_PRIVATE);
editor = sharedPreferences.edit();
String username = sharedPreferences.getString("username", "");
String password = sharedPreferences.getString("password", "");
Log.d(TAG, "Username:"+username+"\npassword:"+password);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
And This my Tab Fragment:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.viewpager, null);
tabLayout = (TabLayout)view.findViewById(R.id.tabs);
viewPager = (ViewPager)view.findViewById(R.id.viewpager);
viewPager.setAdapter(new Adapterbaru(getChildFragmentManager()));
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
return view;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
public class Adapterbaru extends FragmentPagerAdapter{
public Adapterbaru(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0 : {
return new Fragmentone();
}
case 1 : {
return new FragmentTwo();
}
case 2 : {
return new Fragmenttree();
}
}
return null;
}
@Override
public int getCount() {
return jumlah_tab;
}
@Override
public CharSequence getPageTitle(int position) {
switch(position){
case 0 : {
return "First";
}
case 1 : {
return "Second";
}
case 2 : {
return "Third";
}
}
return null;
}
回答1:
From your
LoginActivity
, pass tabs fragment(FragmentOne, FragmentTwo, FragmentThree)
id as0, 1, 2
throughIntent
.Intent intent = new Intent(LoginActivity.this, MainActivity.class); intent.putExtra("FRAGMENT_ID", 0); startActivity(intent);
In your
MainActivity
, get the fragment id fromIntent
// Inside OnCreate() int fragmentId = getIntent().getIntExtra("FRAGMENT_ID", 0);
From
MainActivity
, showTabFragment
usingFragmentManager
and passfragmentId
toTabFragment
usingBundle
// Inside OnCreate() Bundle bundle = new Bundle(); bundle.putString("TARGET_FRAGMENT_ID", fragmentId); TabFragment tabFragment = new TabFragment(); tabFragment.setArguments(bundle); getSupportFragmentManager().beginTransaction().replace(R.id.confrag, tabFragment).commit();
In
TabFragment
, get target fragment(FragmentOne, FragmentTwo, FragmentThree)
id fromBundle
and finally show desired fragment usingViewPager.setCurrentItem(POSITION)
method.// Inside OnCreateView() int position = getArguments().getInt("TARGET_FRAGMENT_ID"); viewpager.setCurrentItem(position);
Hope this will help~
回答2:
You can check it as
if(isFromLogin){
viewpager.setCurrentItem(specificFragmentPosition);
}
Happy Coding!
回答3:
viewpager.setCurrentItem(posotion);
回答4:
From your LoginActivity pass Intents like this
In LoginActivity.class
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("Fragmentone", 0); //pass zero for Fragmentone.
startActivity(intent);
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("FragmentTwo", 1); //pass zero for FragmentTwo.
startActivity(intent);
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("FragmentThree", 2); //pass zero for FragmentThree.
startActivity(intent);
In MainActivity.class
int value = getIntent().getIntExtra("Fragmentone", 0);
From Your activity pass like this
Bundle bundle = new Bundle();
bundle.putString("key", value);
FragmentOne fragmentOne = new FragmentOne();
fragmentOne.setArguments(bundle);
and inside oncreateView of Fragment please define this
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int strtext = getArguments().getInt("key");
return inflater.inflate(R.layout.fragment, container, false);
}
来源:https://stackoverflow.com/questions/43178728/how-to-intent-into-specific-tab-fragment