0

"saving the all data using onSavedInstanceState(Bundle) but if my app is closed then reopened my apps and how to restore the saved Instance "

package com.example.savedandrestored;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText first,last;
Button submit;
@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    first=findViewById(R.id.firstname);
    last=findViewById(R.id.lastname);
    submit=findViewById(R.id.submit);
    if(savedInstanceState!=null && savedInstanceState.containsKey("firstName") && savedInstanceState.containsKey("lastName")) {
        first.setText(savedInstanceState.get("firstName").toString());
        last.setText(savedInstanceState.get("lastName").toString());
    }
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
 savedInstanceState.putString("firstName",first.getText().toString());
   savedInstanceState.putString("lastName",last.getText().toString());
            onSaveInstanceState(savedInstanceState);
        }
    });
    }
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
     }
}

"it does not restore the data when I open the apps and no new data is executed"

Jasurbek
  • 2,946
  • 3
  • 20
  • 37
  • Possible duplicate of [How to save an Android Activity state using save instance state?](https://stackoverflow.com/questions/151777/how-to-save-an-android-activity-state-using-save-instance-state) – Ananth Jul 08 '19 at 13:24
  • 1
    You cannot restore the current data of activities or fragments when close and reopen the app unless you save it to the database or shared preferences. – seyfullah.bilgin Jul 08 '19 at 13:26

1 Answers1

1

onSaveInstanceState is used for another purpose like recreating activity in rotation or etc.

saving all data using onSavedInstanceState(Bundle) but if my app is closed then reopened my apps

Then you should read the lifecycle of an activity.

But the short answer to your question is

  1. Save your data inside onPause
  2. Retrieve it onResume

Like an example

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //retrieve your data here and update UI here not onCreate 

    }

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

        //Save your data here into shared prefs, or file, or DB (*last not recommended)
    }
}
Jasurbek
  • 2,946
  • 3
  • 20
  • 37