-1

1I have three activities like A,B,C. I move to activity A then to activity b. I navigate to activity B from activity A.

I want to navigate to activity A from activity B. When I return to activity B, it should be in the same state as I have left (with filled views). I have taken user input in both activities and want to save it

activity A

3 Answers3

0

For store your activity view You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter.

Have look this

Happy coding!!

Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
  • Thanks, my situation is like this: I filled input fields in activity A and navigate to activity B using NEXT button. I filled input fields in activity B. Activity B has the previous button to navigate back to activity A. I do so. When I again click NEXT button from activity A, activity B is restarted. I don't want this, I want to activity B input fields filled in. Is there any way to resume activity B from activity A. – user8140661 Nov 07 '17 at 12:24
0

It seems to me that would come in handy for you controlling those screens through Fragments with Viewpagers instead of Activities. However if you still need Activities, you can make use of bundles or having those entities kept inside your application context which is in common context among activities (which I do not advise)

Victor Hugo Montes
  • 1,270
  • 1
  • 17
  • 28
  • Thanks, my situation is like this: I filled input fields in activity A and navigate to activity B using NEXT button. I filled input fields in activity B. Activity B has the previous button to navigate back to activity A. I do so. When I again click NEXT button from activity A, activity B is restarted. I don't want this, I want to activity B input fields filled in. Is there any way to resume activity B from activity A. – user8140661 Nov 07 '17 at 12:24
0

Once you are moving from one activity to another activity the edited field will be available if you are navigate from the back stack not by the Intent.

example

Back Stack - >pressing the back button it will call the activity previous you are in.

Intent - > startActivity(new Intent(ActivityA.this,ActivityB.class));

If you use intent for changing the activity it will recreate the activity so the edited fields will not be available ..

so far if you are want to store the fields once user came back you need to use shared preferences..

Refer the official documentation

I can give you a sample to store the value in shared preference and retrieve from it.

public class CommonUtil {

public static void setUserName(String userName,Context context){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("user_name",userName);
    editor.apply();
}

public static String getUserName(Context context){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    return sharedPreferences.getString("user_name",null);
}}

To set values form activity

 CommonUtil.setUserName("user",getApplicationContext());
 CommonUtil.getUserName(getApplicationContext());

Once the user going to leave i will update the value from the textfields

Update the CommonUtil method..

where the key will be what the username,password

where the value will be taken from the textfields

public static void setValues(String key,String value, Context context){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    final SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.remove(key);
    editor.putString(key,value);
    editor.apply();
}

public static String getValues(String key,Context context){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    return sharedPreferences.getString(key,null);
}

if you prefer looping that is also possible

final solution for your problem..

final EditText username,surname,middlename,dob,email,password;
    Button get,save;

    final String[] keyset = new String[]{"username","surname","middlename","dob","email","password"};

    username = (EditText) findViewById(R.id.username);
    surname = (EditText) findViewById(R.id.surname);
    middlename = (EditText) findViewById(R.id.middlename);
    dob = (EditText) findViewById(R.id.dob);
    email = (EditText) findViewById(R.id.email);
    password = (EditText) findViewById(R.id.password);

    final EditText[] editTexts = {username,surname,middlename,dob,email,password};

    get = (Button) findViewById(R.id.get);
    save = (Button) findViewById(R.id.save);

    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for (int i =0 ; i<6 ;i++){

                CommonUtil.setValues(keyset[i],editTexts[i].getText().toString(),getApplicationContext());

                Log.d("got",editTexts[i].getText().toString());
            }
        }
    });

    get.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for (int i =0 ; i<6 ;i++){
                CommonUtil.getValues(keyset[i],getApplicationContext());

                Log.d("got",CommonUtil.getValues(keyset[i],getApplicationContext()));
            }
        }
    });
  • Thanks, my situation is like this: I filled input fields in activity A and navigate to activity B using NEXT button. I filled input fields in activity B. Activity B has the previous button to navigate back to activity A. I do so. When I again click NEXT button from activity A, activity B is restarted. I don't want this, I want to activity B input fields filled in. Is there any way to resume activity B from activity A. – user8140661 Nov 07 '17 at 12:14
  • You know when user press the previous or next button when click save the values in shared preferences once user back get the values from shared preference check if the value is not null mandatory.. – Ashokkumar Adichill Nov 07 '17 at 12:16
  • you can also pass the needed data through intent if user in A activity i need to pass the data to B Activity what i can do i simply get the B Activity data from the shared preference and i sent the data to B Activity through Intent.putExtra – Ashokkumar Adichill Nov 07 '17 at 12:18
  • I have used intent. I want to resume activity B from activity A. Using shared preferences is the only way? I have many input fields in activity B, shared preferences would require alot of code to write – user8140661 Nov 07 '17 at 12:22
  • Just give some time i will work the shortest way to save the value in shared preferences – Ashokkumar Adichill Nov 07 '17 at 12:26
  • I have edited the answer i have using only one method for updating the values and retreive from it... – Ashokkumar Adichill Nov 07 '17 at 12:43