I am currently developing an android music reading app in Android Studio. The language I am using is Java but I am a beginner (sorry for my English too).
The app has several exercises. Ideally, people can click on the next or back button to see one exercise or another. The problem is that I am also looking for the user, even if he exits the application, to continue in the last exercise (image) seen.
I have read about the cycle of activities and I understand it, but since I am a beginner in the language I cannot do what I need to do. I have learned that OnSaveInstanceState or OnRestoreInstanceState do not solve my problem and that the solution may be by using SharedPreferences but I don't know how to apply it to the code I currently have. With SharedPreferences I hope I can save and restore the last imageview seen and that I can continue from there by clicking next or back.
This is the current code and it allows me to: show the initial exercise and go from one exercise to another.
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private int image_index = 0 ;
private static final int MAX_IMAGE_COUNT = 3;
private int[] mImageIds = {
R.raw.image1,
R.raw.image2,
R.raw.image3};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showImage();
}
private void showImage() {
ImageView imgView = (ImageView) findViewById(R.id.myimage);
imgView.setImageResource(mImageIds[image_index]);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case (R.id.previous_btn):
image_index--;
if (image_index == -1) {
image_index = MAX_IMAGE_COUNT - 1;
} else {
}
showImage();
break;
case (R.id.next_btn):
image_index++;
if (image_index == MAX_IMAGE_COUNT) {
image_index = 0;
} else {
}
showImage();
break;
}
}
}
This is the xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:background="@android:color/background_light"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="@font/gilroyextrabold"
android:text="Ejercicios"
android:textColor="@android:color/black"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/myimage"
android:layout_width="wrap_content"
android:layout_height="370dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<LinearLayout
android:id="@+id/linearLayout5"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/myimage">
<Button
android:id="@+id/previous_btn"
android:layout_width="190dip"
android:layout_height="wrap_content"
android:background="@drawable/btnatsslf"
android:onClick="onClick" />
<Button
android:id="@+id/next_btn"
android:layout_width="190dip"
android:layout_height="wrap_content"
android:background="@drawable/btnsgtslf"
android:onClick="onClick" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</ScrollView>
As I don't have that much knowledge, I seek your help and I also receive alternatives.
put these lines after setContentView
, but before showImage
in onCreate
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
image_index = sp.getInt("image_index", image_index);
and save this value inside showImage
just after setImageResource
line
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
sp.edit().putInt("image_index", image_index).apply();
you may keep reference to SharedPreferences sp
as class member, so it won't be needed to obtain every time when showImage
is called (also ImageView
should be stored same way without need of findViewById
everytime). also the key ("image_index"
) may be stored as some private static final String
class member
also remember for future use: this is index in some array you have declared static. when you change it, e.g. stored value is 2 and you cut last item in images array then user get index out of bounds exception after app update installation, so inside onCreate
check that this value is in range of array (just for safety)