EagerToLearn
EagerToLearn

Reputation: 675

ProgressBar's progress display wrongly when rotated or resized

I'm using ProgressBar to display the percentage of questions done in my application.

However I'm facing a strange issue, when the activity's onCreate's method get called again (rotate, resize ..), the ProgressBar's progress display become very weird.

I debug the code, but everything is fine, the setMax() and setProgress()'s value are also correct.

First Load :

enter image description here

Resize screen :

enter image description here

My onCreate method :

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_practice_type_list);

        LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);

        Bundle b = getIntent().getExtras();
        int practiceType = b.getInt("practiceType");
        int level = b.getInt("level");

        DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
        databaseAccess.open();
        ArrayList<BookInfoByTypeModel> bookInfo = databaseAccess.getBookInfoByType(level, practiceType);
        databaseAccess.close();

        ScrollView scrollView = (ScrollView)findViewById(R.id.practice_type_layout_scrollview);
        LinearLayout topParent = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;

        topParent.setLayoutParams(params);
        topParent.setOrientation(LinearLayout.VERTICAL);

        View viewTypeItem;
        TextView tvTypeThumbnail;
        TextView tvTypeName;
        TextView tvTypeCount;
        ProgressBar pbTypeProgressPercent;
        TextView tvTypeProgressText;

        for (int i = 0; i < bookInfo.size(); i++) {
            BookInfoByTypeModel book = bookInfo.get(i);

            viewTypeItem = inflater.inflate(R.layout.type_item, scrollView, false);
            tvTypeThumbnail = (TextView) viewTypeItem.findViewById(R.id.type_thumbnail);
            tvTypeName = (TextView) viewTypeItem.findViewById(R.id.type_name);
            tvTypeCount = (TextView) viewTypeItem.findViewById(R.id.type_count);
            pbTypeProgressPercent = (ProgressBar) viewTypeItem.findViewById(R.id.type_progress_percent);
            tvTypeProgressText = (TextView) viewTypeItem.findViewById(R.id.type_progress_text);

            switch (book.getType()){
                case 0:
                    tvTypeThumbnail.setText("字");
                    tvTypeName.setText("漢字・語彙");
                    break;
                case 1:
                    tvTypeThumbnail.setText("漢");
                    tvTypeName.setText("漢字");
                    break;
                case 2:
                    tvTypeThumbnail.setText("語");
                    tvTypeName.setText("語彙");
                    break;
                case 3:
                    tvTypeThumbnail.setText("文");
                    tvTypeName.setText("文法");
                    break;
                case 4:
                    tvTypeThumbnail.setText("読");
                    tvTypeName.setText("読解");
                    break;
                case 5:
                    tvTypeThumbnail.setText("聴");
                    tvTypeName.setText("聴解");
                    break;
                case 6:
                    tvTypeThumbnail.setText("総");
                    tvTypeName.setText("総合");
                    break;
            }

            tvTypeCount.setText("問題集数:" + book.getBookCount());
            tvTypeProgressText.setText(book.getProgressCount() + "/" + book.getBookCount());
            pbTypeProgressPercent.setMax(book.getBookCount());
            pbTypeProgressPercent.setProgress(book.getProgressCount());
            topParent.addView(viewTypeItem);
        }

        scrollView.addView(topParent);
    }

What am I doing wrong here?

Upvotes: 0

Views: 115

Answers (1)

Pawel
Pawel

Reputation: 17248

You're inflating multiple views with same ID into the layout. Usually Views automatically restore their state, but in that case the state is overriden by "last" view with given ID in the layout so all the views (with that particular ID) end up "restoring" from it which causes save state corruption.

State restoration is happening in (super) onRestoreInstanceState method which is called after onCreate, that's why values you set there are lost.

To prevent that behavior disable state saving with setSaveEnabled(false) on views you're adding to the layout.

Upvotes: 1

Related Questions