The main sent a String value to the second screen through the int. On the second screen, three tabs and a viewfager are used, and each tab is made up of fragments. However, when you click a button on the second screen or something like that, it is possible to hand over the String value from the main to the fragment again, but if there is no click event or some event, the String value from the main does not pass to the fragment. What kind of problem?
Intent intent = getIntent();
if(intent != null) {
token = intent.getStringExtra("token");
uid = intent.getStringExtra("uid");
client = intent.getStringExtra("client");
if(tab1 != null) {
tab1 = (FirstTabFragment) findFragmentByPosition(0);
tab1.setAuth(token, uid, client);
}
Log.e("fff", "aa - " + token);
Log.e("fff", "aa - " + uid);
Log.e("fff", "aa - " + client);
//asdfa(token, uid, client);
}
If you do this on the second screen, the value will not be passed to the fragment.
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(tab1 != null) {
tab1 = (FirstTabFragment) findFragmentByPosition(0);
tab1.setAuth(token, uid, client);
}
}
});
}
But in this way, if you create a random button on the second screen and hand it over when you click, it has been passed on to the fragment. By the way, I'm going to hand it over to the fragment right after the String value is passed from the main to the second screen. You don't click on a button or anything.
What should I do? <
android fragment value android-viewpager tablayout
The simplest way in the current code seems to be to get the content information of the activity directly from FirstTabFragment, as follows:
String token = getActivity().getIntent().getStringExtra("token");
String uid = getActivity().getIntent().getStringExtra("uid");
String client = getActivity().getIntent().getStringExtra("client");
Add the above code to a function such as onCreateView() in the fragment to see if the value is delivered correctly.
private FirstTabFragment tab1;
private SecondTabFragment tab2;
private ThirdTabFragment tab3;
Intent intent = getIntent();
if(intent != null) {
token = intent.getStringExtra("token");
uid = intent.getStringExtra("uid");
client = intent.getStringExtra("client");
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle ("Board");
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(""));
tabLayout.addTab(tabLayout.newTab().setText(""));
tabLayout.addTab(tabLayout.newTab().setText(""));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) findViewById(R.id.viewpager);
adapter = new ViewPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setOffscreenPageLimit(3);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
if(tab1 != null) {
tab1 = (FirstTabFragment) findFragmentByPosition(0);
tab1.setAuth(token, uid, client);
}
Log.e("fff", "aa - " + token);
Log.e("fff", "aa - " + uid);
Log.e("fff", "aa - " + client);
}
Modifying the code in this way does not work (tab1 == null). However, when you click the button, tab1!= null. What did I do wrong?
© 2024 OneMinuteCode. All rights reserved.