Home > Software design >  How to refresh an activity in Android?
How to refresh an activity in Android?

Time:05-20

I'm learning android app development by creating a Wordle like app. The problem is the properties of the views do get changed but they don't get reflected in main_activity. I can't find a way to refresh the activity. I've tried the solutions to these questions

Android - How to refresh an activity

Programmatically relaunch/recreate an activity?

But none of them worked for me.

MainActivity.java:

package com.example.wordlepromax;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.content.Intent;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends AppCompatActivity {

    private String letter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ConstraintLayout layout = findViewById(R.id.attempt1);
        Letter currView = (Letter) layout.getChildAt(0);
        if (letter != null) {
            currView.setLetter(letter);
        }
    }

    public void testSet(View view) {
        letter = "N";
        recreate();
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        letter = savedInstanceState.getString("view_Letter");
    }

    // invoked when the activity may be temporarily destroyed, save the instance state here
    @Override
    public void onSaveInstanceState(Bundle outState) {
        // call superclass to save any view hierarchy
        outState.putString("view_Letter", letter);
        super.onSaveInstanceState(outState);
    }

}

main_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@ id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@ id/attempt1"
        android:layout_width="380dp"
        android:layout_height="94dp"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.483"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.example.wordlepromax.Letter
            android:id="@ id/letter10"
            android:layout_width="65dp"
            android:layout_height="65dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="16dp"
            app:layout_constraintEnd_toStartOf="@ id/letter11"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.example.wordlepromax.Letter
            android:id="@ id/letter11"
            android:layout_width="65dp"
            android:layout_height="65dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toStartOf="@ id/letter12"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@ id/letter10"
            app:layout_constraintTop_toTopOf="parent" />

        <com.example.wordlepromax.Letter
            android:id="@ id/letter12"
            android:layout_width="65dp"
            android:layout_height="65dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toStartOf="@ id/letter13"
            app:layout_constraintTop_toTopOf="parent" />

        <com.example.wordlepromax.Letter
            android:id="@ id/letter13"
            android:layout_width="65dp"
            android:layout_height="65dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toStartOf="@ id/letter14"
            app:layout_constraintTop_toTopOf="parent" />

        <com.example.wordlepromax.Letter
            android:id="@ id/letter14"
            android:layout_width="65dp"
            android:layout_height="65dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <Button
        android:id="@ id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:hint="@string/set_letter_test"
        android:onClick="testSet"
        android:textColorHint="#000000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/attempt1" />

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

From the javadoc for onRestoreInstanceState :

This method is called after onStart when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState.

In your case it does not work, because you're setting letter variable to View inside of onCreate method, at this point onRestoreInstanceState just was not called and letter variable was not updated(restored).

Move this line to the begining of onCreate method. In this case you can remove onRestoreInstanceState.

letter = savedInstanceState.getString("view_Letter");
  • Related