Ok, So I followed a tutorial from youtube Android Tab Tutorial [Android Studio Tab Fragments].
I made some very basic modifications, on each tab there's a Button and a TextView. When I click the buttons, the Text on the TextView changes.
The problem is when changing screen orientation, the text of the TextView is reverted to its default value.
I followed the instructions of Google's Activity Lifecycle guide, but my app still fails to retain the value on the TextView when changing the screen orientation or switching between the tabs. I am guessing it has something to do with the way the Fragments are created, but I can't figure out what the problem is exactly or how to fix it.
The code is as below:
MainActivity Class:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private SectionsPageAdapter mySectionsPageAdapter;
private ViewPager myViewPager;
SectionsPageAdapter myAdapter = new SectionsPageAdapter(getSupportFragmentManager());
private Fragment Tab1 = new Tab1Fragment();
private Fragment Tab2 = new Tab2Fragment();
private Fragment Tab3 = new Tab3Fragment();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: Starting.");
mySectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
myViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(myViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(myViewPager);
}
private void setupViewPager(ViewPager viewPager)
{
myAdapter = new SectionsPageAdapter(getSupportFragmentManager());
myAdapter.addFragment(new Tab1Fragment(), "Tab 1");
myAdapter.addFragment(new Tab2Fragment(), "Tab 2");
myAdapter.addFragment(new Tab3Fragment(), "Tab 3");
viewPager.setAdapter(myAdapter);
}}
Class SectionsPageAdapter:
public class SectionsPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> myFragmentsList = new ArrayList<>();
private final List<String> myFragmentTitlesList = new ArrayList<>();
public void addFragment(Fragment myFragment, String fragmentTitle)
{
myFragmentsList.add(myFragment);
myFragmentTitlesList.add(fragmentTitle);
}
public SectionsPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return myFragmentTitlesList.get(position); // super.getPageTitle(position);
}
@Override
public Fragment getItem(int position) {
return myFragmentsList.get(position); // null;
}
@Override
public int getCount() {
return myFragmentsList.size(); // 0;
}}
Class Tab1Fragment :
public class Tab1Fragment extends Fragment
{
private static final String TAG = "Tab1Fragment";
private Button Button1;
private TextView Textview1;
static final String labelText = "";
static String labelTextValue = "";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
View viewOne = inflater.inflate(R.layout.tab1_fragment, container, false);
Button1 = (Button) viewOne.findViewById(R.id.button1);
Textview1 = (TextView) viewOne.findViewById(R.id.textView1);
Button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
labelTextValue = "This text was changed from the button!";
Textview1.setText(labelTextValue);
}
});
return viewOne; // super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
savedInstanceState.putString(labelText, (labelTextValue));
} else {
// Probably initialize members with default values for a new instance
}
}
@Override
public void onResume() {
super.onResume(); // Always call the superclass method first
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the TextView's value
savedInstanceState.putString(labelText, (labelTextValue));
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}}
The code for the other two fragments is the same, I only change the number '1' into 2 and 3.