0

In my Android Application I have created three activities. each one contain some inputs fields and button "Next Page" except the final one the button in it responsible for submission. My problem is when user fill all the inputs and before clicking on submit button if he want to go back using the phone default integrated back button and then after checking the first page when he click on "Next Button" in order to go to the page two the inputs in the page two disappeared.(because as you know the activity will restart so all inputs data will be removed)

Is there any way to save the data.

I did saved it when go forward from first page to the third one by using:

Intent intent = new Intent(CreateViolationPageOne.this,CreateViolationPageTwo.class);
                String allData = inputStreet.getText().toString()+","+inputVehicle.getText().toString()+","+inputBrand.getText().toString()+
                        inputColor.getText().toString()+","+inputNumber.getText().toString();
                intent.putExtra(CreateViolationPageTwo.ALL_DATA, allData);
                startActivity(intent);

Note:

I did not use

finish() 

after

StartActivity(intent)

Moreover I did use onSaveInstanceState in my code but the problem still happening:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    if(!inputStreet.getText().toString().isEmpty())
        savedInstanceState.putString("inputStreet", inputStreet.getText().toString());
    if(!inputVehicle.getText().toString().isEmpty())
        savedInstanceState.putString("inputVehicle", inputVehicle.getText().toString());
    if(!inputBrand.getText().toString().isEmpty())
        savedInstanceState.putString("inputBrand", inputBrand.getText().toString());
    if(!inputColor.getText().toString().isEmpty())
        savedInstanceState.putString("inputColor", inputColor.getText().toString());
    if(!inputNumber.getText().toString().isEmpty())
        savedInstanceState.putInt("inputNumber", Integer.parseInt(inputNumber.getText().toString()));
}

@Override
protected void onResume() {
    super.onResume();
}

@Override
protected void onPause() {
    super.onPause();
}

and then in the onCreate method I added:

if (savedInstanceState != null) {
        if(savedInstanceState.containsKey("inputStreet"))
            inputStreet.setText(savedInstanceState.getString("inputStreet"));
        if(savedInstanceState.containsKey("inputVehicle"))
            inputVehicle.setText(savedInstanceState.getString("inputVehicle"));
        if(savedInstanceState.containsKey("inputBrand"))
            inputBrand.setText(savedInstanceState.getString("inputBrand"));
        if(savedInstanceState.containsKey("inputColor"))
            inputColor.setText(savedInstanceState.getString("inputColor"));
        if(savedInstanceState.containsKey("inputNumber"))
            inputNumber.setText(Integer.toString(savedInstanceState.getInt("inputNumber")));
    }

This is my first android application and I am new to Android Studio.

Ali Hamed
  • 316
  • 1
  • 1
  • 12
  • You need to save and restore the data. Use `onSaveInstanceState` : https://stackoverflow.com/questions/16769654/how-to-use-onsaveinstancestate-and-onrestoreinstancestate – Alexander Hoffmann Jan 09 '23 at 22:58
  • @AlexanderHoffmann I did that but the problem still happened. I will add the code to the question. – Ali Hamed Jan 10 '23 at 12:22

1 Answers1

0

I discovered the answer to my issue. I discovered that creating a class with fields that correspond to the data types of the inputs was the most effective approach. As an illustration, I have input for street in my activity, thus I made a private String street field in my class to retain the input's data.

My class looks like this:

public class AllViolationData implements Serializable {
private static AllViolationData violationData;
private String numberOne;
private String numberTwo;
private String inputStreet;
private String inputVehicle;
//the rest of my data fields 
}

But the trick was to create a static instance of this class inside it with getter an setter for it

public static void setViolationData(AllViolationData violationData) {
    AllViolationData.violationData = violationData;
}

public static AllViolationData getViolationData() {
    if(violationData==null){
        violationData = new AllViolationData();
    }
    return violationData;
}

Now, I utilize getVioationData to save the data inside of it when a user clicks the next button to move to the next page, and I then start the subsequent activity when the data has been saved.

Finally, I used the same object in each activate by placing my code within the onCreate function, and I didn't have to worry about the static notion because the object will be null the first time I start an activate because I always set it to null when I submit data.

It may not be the ideal solution, but it accomplishes the job.

Ali Hamed
  • 316
  • 1
  • 1
  • 12