본문 바로가기
안드로이드 자바

[Android][Java] Activity 생명주기 onStart() 활용해 RecyclerView 데이터 갱신하기

by teamnova 2023. 6. 10.

먼저 Android Activity 생명주기에 대한 내용입니다.

https://stickode.tistory.com/5

 

[Java][Android] 안드로이드 액티비티 생명주기

액티비티란? 사용자에게 UI가 있는 화면을 제공하는 앱 컴포넌트입니다. 안드로이드는 반드시 하나 이상의 액티비티를 포함하고 있고 , 액티비티는 생명주기 관련 메서드를 재정의하여 기능을

stickode.tistory.com

 

위 내용에서 저는 이런 생명주기별로 호출되는 메소드들을 어떤 상황에서 주로 사용하는지 알려드리겠습니다.

- onCreate()

onCreate()에서는 액티비티가 실행될 동안 한번만 발생해야 하는 기본 애플리케이션 로직을 작성해야 합니다. 예를 들어 클래스 변수를 인스턴스화하거나 뷰바인딩을 통해 레이아웃 객체들을 호출할 수 있는 변수를 초기화 할 때 사용합니다.

 

- onStart()

액티비티가 사용자에게 보이기 직전에 호출이 되고 사용자와 상호작용을 준비하는 로직을 준비해야 합니다. 주로 UI를 통해 상호작용하는 코드를 초기화 할 때 사용합니다.

 

- onRestart()

 onStart()와의 차이점은 onStop()가 되었다가 다시 돌아왓을 때만 실행되는 메소드로 다시 시작되었을 때 필요한 기능들을 작성할 때 사용할 수 있습니다.

 

- onResume()

사용자와 실제로 상호작용을 하기위해서 필요한 모든 기능을 활성화하는 코드를 작성할 때 사용합니다. 예를 들어 다른 앱과 공유할 수 없는 카메라 기능을 활성활 하는 코드를 해당 메소드에서 사용할 수 있습니다.

 

- onPause()

액티비티가 포커스를 잃어버리게 될 때 호출되는 메소드로 잠시 후 다시 시작할 작업을 일시중지하거나 조정할 때 사용할 수 있다. 예를 들어 onResume 때 활성화하는 카메라 기능을 onPause 때 비활성화 하는 코드를 작성할 수 있다.

 

- onStop()

액티비티가 완전히 보이지 않을 때 호출되는 메소드로 필요하지 않은 리소스를 해제하거나 조정하는 코드를 작성할 때 사용합니다.  또 데이터베이스에 저장할 적절한 시기를 찾기 못했다면 onStop  때 저장하는 코드를 작성할 수 있습니다.

 

- onDestroy()

액티비티가 완전히 종료되었을 때 호출되는 메소드로 이전에 메소드들에서 해제되지 않은 모든 리소스를 해제해야 한다. 또 onDestroy의 호출은 보장되지 않기 때문에 꼭 실행해야만 하는 코드라면 onDestroy에 작성하는 것은 위험할 수 있습니다.

 

Activity의 생명주기를 이용해 저희는 사용자가 다른 활동 또는 다른 액티비티를 실행했다가 다시 해당 액티비티로 돌아올 때마다 데이터를 갱신할 수 있습니다.

만약 제가 RecyclerView에 데이터를 갱신하는 코드를 onCreate 메소드 내부에서 작성하였다면 제가 다른 액티비티나 앱으로 이동한 뒤 다시 해당 액티비티로 돌아왔을 때 다시 메소드가 호출되지 않아 데이터가 갱신이 되지 않습니다.

하지만 onStart() 메소드 내부에 RecyclerView 데이터를  갱신하는 코드를 작성하면 언제든지 해당 액티비티로 돌아왔을 때 갱신된 데이터를 확인할 수 있습니다.

 

그러니 이번엔 OnStart 메소드를 활용해 액티비티 내 RecyclerView에 있는 데이터를 해당 액티비티를 실행하거나 돌아왔을 때 계속 갱신해보겠습니다.

 

activity_main.xml (데이터 리스트 표시 Activity Layout)

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="리스트"
        android:textColor="@color/black"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recy"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="100dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <Button
        android:id="@+id/move"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginEnd="100dp"
        android:text="데이터 추가하러 가기"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

MainActivity.java (RecyclerView에 데이터 표시하는 Activity)

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    public static ArrayList<String> data; // 리스트에 들어갈 데이터 리스트 변수
    Adapter adapter; // 리사이클러뷰에 연결할 Adapter 변수

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

        // 초기 데이터 설정
        data = new ArrayList<>();
        for(int i = 0; i < 5; i++){
            data.add("test settings data"+i);
        }

        // 데이터 추가화면을 실행하는 Button 초기화 및 클릭 리스너 설정
        Button moveToNext = (Button)findViewById(R.id.move);
        moveToNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 데이터 추가 액티비티 실행
                Intent intent = new Intent(MainActivity.this, AddActivity.class);
                startActivity(intent);
            }
        });

        // 리스트 데이터를 표시할 RecyclerView 초기화 및 LinearLayoutManager 설정
        RecyclerView recyclerView = findViewById(R.id.recy);
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        
        // 어댑터 생성 후 RecyclerView에 설정
        adapter = new Adapter();
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // RecyclerView 리스트 업데이트 메소드 실행
        adapter.setData(data);
    }
}

 

activity_add.xml (데이터 추가 Activity Layout)

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AddActivity">

    <EditText
        android:id="@+id/inputData"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="228dp"
        android:ems="10"
        android:gravity="center"
        android:hint="리스트에 추가할 데이터 입력"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/addBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        android:text="추가"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/inputData" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

AddActivity.java (RecyclerView에 사용될 데이터 추가하는 Activity)

import static com.teamnova.recyclerviewonstart.MainActivity.data;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddActivity extends AppCompatActivity {

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

        // 리스트에 저장할 데이터를 입력할 EditText 변수 초기화
        EditText inputData = (EditText)findViewById(R.id.inputData);
        
        // 데이터를 저장하기 위해 누르는 Button 변수 초기화 및 클릭 리스너 설정
        Button addBtn = (Button) findViewById(R.id.addBtn);
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String dataStr = inputData.getText().toString(); // 입력창에 있는 문자열 변수에 저장

                if(dataStr.trim().equals("")){
                    // 아무런 데이터도 입력하지 않으면 알림 메세지 생성
                    Toast.makeText(AddActivity.this, "데이터를 입력해주세요", Toast.LENGTH_SHORT).show();
                }else {
                    // 데이터를 입력했을 경우
                    inputData.setText(""); // 입력창 지우기

                    // MainActivity에서 초기화된 데이터 리스트에 새로운 데이터 추가
                    data.add(dataStr);

                    // 데이터가 추가되었습니다
                    Toast.makeText(AddActivity.this, "데이터가 추가되었습니다", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

 

itemview.xml (리스트 내 ViewHolder Layout)

<?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:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:text="TextView"
        android:textColor="@color/black"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

Adapter.java (RecyclerView Adapter class)

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

    ArrayList<String> dataList; // 리스트에 표시할 받아온 데이터 변수

    @NonNull
    @Override
    public Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 뷰홀더 생성
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemview, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Adapter.ViewHolder holder, int position) {
        // 뷰홀더에 데이터 바인드하기
        holder.getData().setText(dataList.get(position));
    }

    @Override
    public int getItemCount() {
        // 현재 가진 데이터 수 반환
        return dataList.size();
    }

    // 데이터를 다시 입력해주는 메소드
    void setData(ArrayList<String> mData){
        dataList = null;
        dataList = new ArrayList<>();
        for(int i = 0; i < mData.size(); i++ ){
            dataList.add(mData.get(i));
        }

        // 데이터 변경 후 업데이트 실행
        notifyDataSetChanged();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        // 뷰홀더에서 데이터를 표시할 TextView 변수
        TextView data;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            // 변수 초기화
            data = (TextView)itemView.findViewById(R.id.data);
        }

        // 데이터 반환 메소드
        TextView getData(){
            return data;
        }
    }
}

 

결과 영상입니다.