'How to save current state of the Activity and a Fragment
I am new in Android development, I am building an onboarding screen and I created MainActivity Which creates ViewPager2 and sets an adapter FragmentStateAdapter my ISSUE is when I change the theme to dark the app crashes the error is
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zamw.onboarding/com.zamw.onboarding.MainActivity}: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.zamw.onboarding.OnboardingFragment: could not find Fragment constructor
Below is My MainActivity and Fragment files
package com.zamw.onboarding;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final int NUM_PAGES = 4;
private ViewPager2 viewPager;
private FragmentStateAdapter pagerAdapter;
private CardView[] dots;
private TextView textView;
private LinearLayout.LayoutParams layoutParams;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.pager);
textView = findViewById(R.id.skip);
dots = new CardView[]{findViewById(R.id.dot1),
findViewById(R.id.dot2),
findViewById(R.id.dot3),
findViewById(R.id.dot4)};
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
for (int i=0; i<4; i++)
{
layoutParams = (LinearLayout.LayoutParams) dots[i].getLayoutParams();
if (i==position) {
dots[i].setCardBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
layoutParams.height=dpToPx(8);
layoutParams.width=dpToPx(20);
dots[i].setLayoutParams(layoutParams);
if (position==3){textView.setText("GET STARTED");}
}
else {
dots[i].setCardBackgroundColor(getResources().getColor(R.color.red));
layoutParams.height=dpToPx(8);
layoutParams.width=dpToPx(8);
dots[i].setLayoutParams(layoutParams);
textView.setText("SKIP");
}
}
}
});
pagerAdapter = new ScreenSlidePagerAdapter(this);
viewPager.setAdapter(pagerAdapter);
}
public int dpToPx(int dp) {
DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}
private class ScreenSlidePagerAdapter extends FragmentStateAdapter {
public ScreenSlidePagerAdapter(FragmentActivity fa) {
super(fa);
}
@Override
public Fragment createFragment(int position) {
OnboardingFragment fragment = new OnboardingFragment(position);
return fragment;
}
@Override
public int getItemCount() {
return NUM_PAGES;
}
}
}
Fragment
package com.zamw.onboarding;
import android.os.Bundle;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class OnboardingFragment extends Fragment {
private int infoId;
public OnboardingFragment(int infoId){
this.infoId=infoId;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_onboarding, container, false);
}
@Override
public void onStart() {
super.onStart();
View view = getView();
if (view!=null){
Informations informations = Informations.informations[infoId];
ImageView imageView = view.findViewById(R.id.imageView);
imageView.setImageResource(informations.getImageID());
TextView title = view.findViewById(R.id.textTitle);
title.setText(informations.getName());
TextView description = view.findViewById(R.id.textDescription);
description.setText(informations.getDescription());
}
}
}
I want to know how to handle this kind of problems, how to save current state of the screen?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
